Reputation: 75
I'm trying to use echo -n
command to write some content to a file without a new line in node's package.json e.g
start: "echo -n hello > my.file"
When I run npm start
, instead of hello
without a new line, it creates -n hello
in my.file
. However it runs correctly in my Terminal(I'm on Mac).
Upvotes: 2
Views: 2764
Reputation: 10157
Depending of your context, you can use printf
instead of echo
, which does not append a \n
char automatically.
https://unix.stackexchange.com/questions/58310/difference-between-printf-and-echo-in-bash
Upvotes: 1
Reputation: 1004
This is due to NPM running scripts with the sh(1)
shell, which does not accept the -n
option. One way of solving this would be to set NPM to use Bash:
npm config set script-shell "/bin/bash"
Documentation for
echo
commandSome shells may provide a builtin echo command which is similar or identical to this utility. Most notably, the builtin echo in sh(1) does not accept the -n option. Consult the builtin(1) manual page.
Documentation for NPM run-scripts
The actual shell your script is run within is platform dependent. By default, on Unix-like systems it is the /bin/sh command, on Windows it is the cmd.exe. The actual shell referred to by /bin/sh also depends on the system. As of [email protected] you can customize the shell with the script-shell configuration.
Here's also a related StackOverflow answer: "echo -n" works fine when executing script with bash, but not with sh
Upvotes: 1