I am writing a bash script, and I want to echo a string, but without a newline automatically added to the end. Reading the man page it says the flag is -n
.
The problem is, when I do:
echo -n "My string is here"
The output in the bash script is:
-n My string is here
Any idea why the -n
flag is being outputted instead of processed.
Check the shell you are using with:
If it is
/bin/sh
, change to/bin/bash
withchsh
and try again.http://hints.macworld.com/article.php?story=20071106192548833
Works on CentOS, but appears that OSX does not like the -n flag.
echo
is weirdly inconsistent between versions -- different versions of /bin/echo, as well as the builtin versions in various shells (even different versions of the same shell). Some versions understand options (like-n
), some versions understand escape sequences within the string (end with"\c"
to skip the newline). Some versions use a weird mix of the two.If you want consistent behavior, avoid
echo
and useprintf
instead. It's a little more complicated, but it's much more predictable: