Parsing of an escape sequences is not working in scripts

(Marvin Heilemann) #1

What I Wanted to Do

I tried to echo the following script when someone tries to npm test:

"test": "echo -e '\\e[31mError\\e[0m: No tests specified!\n Go totemplates/additiveand runnpm testthere.' && exit 1",

I expected to see the Error coloured in red.

What Happened Instead

-e \e[31mError\e[0m: No tests specified!

Reproduction Steps

Add the above snippet to the scripts section

Platform Info

$ npm --versions
{ npm: '5.6.0',
  ares: '1.10.1-DEV',
  cldr: '32.0',
  http_parser: '2.8.0',
  icu: '60.1',
  modules: '57',
  napi: '3',
  nghttp2: '1.32.0',
  node: '8.11.4',
  openssl: '1.0.2p',
  tz: '2017c',
  unicode: '10.0',
  uv: '1.19.1',
  v8: '6.2.414.54',
  zlib: '1.2.11' }
$ node -p process.platform

(Lars Willighagen) #2

Seems like sh acts differently than I would expect.

$ sh -c "echo -e 'fo\u0308o'"
-e fo\u0308o

$ echo -e 'fo\u0308o'

$ bash -c "echo -e 'fo\u0308o'"

Edit: no, that’s expected behavior. The built-in echo in dash doesn’t have a -e flag.

Workaround: you can set a different shell (like bash) manually with npm config set script-shell $PATH.

(Marvin Heilemann) #3

Oh okay, I’m sad to hear that. Will this be implemented to dash?
Thanks for the workaround and the fast response!

(Lars Willighagen) #4

No, it’s defined like that in the POSIX standard (link) and use of printf is recommended instead. More info.

(Marvin Heilemann) #5

Thank worked!! Thanks :slight_smile:

(system) #6

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.