'echo' without newline in a shell script

qwertz picture qwertz · Jun 25, 2012 · Viewed 624.8k times · Source

I have a problem with echo in my script:

echo -n "Some string..."

prints

-n Some string...

and moves to the next line. In the console it's working correcly without newline:

Some string...

Answer

Keith Thompson picture Keith Thompson · Jun 25, 2012

There are multiple versions of the echo command, with different behaviors. Apparently the shell used for your script uses a version that doesn't recognize -n.

The printf command has much more consistent behavior. echo is fine for simple things like echo hello, but I suggest using printf for anything more complicated.

What system are you on, and what shell does your script use?