Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"echo -n" prints "-n"

Tags:

sh

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... 
like image 599
qwertz Avatar asked Jun 25 '12 16:06

qwertz


People also ask

What does echo N mean?

Some implementations use echo -n message to tell echo not to append a newline; others don't have -n , so you have to use echo message \c to do the same thing.

How do I print N in bash?

Printing Newline in Bash The most common way is to use the echo command. However, the printf command also works fine. Using the backslash character for newline “\n” is the conventional way.

Why do we use echo with N?

Linux echo Command Options -n option can be used to remove the '\n' (newline character) from the output. By default, echo includes '\n' at the end of every string it outputs.

How do you echo without newline?

The best way to remove the new line is to add '-n'. This signals not to add a new line. When you want to write more complicated commands or sort everything in a single line, you should use the '-n' option. So, it won't print the numbers on the same line.


1 Answers

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?

like image 107
Keith Thompson Avatar answered Sep 27 '22 01:09

Keith Thompson