Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'echo' newline suppression [duplicate]

Why doesn't $echo '-n' write -n on terminal although -n is written within single quotes?

like image 714
Happy Mittal Avatar asked Apr 15 '10 12:04

Happy Mittal


People also ask

Is there a newline at the end of the echo statement?

Fortunately, this is becoming less of a problem, but it is worth being aware of, particularly if you are writing scripts for older Unix systems. When you use the echo statement, a newline is added at the end of the command. That is to say, if your script looks like this:

How to echo the same command without adding a new line?

So, if you type in ‘echo 1’ as the command in your prompt, you’ll get 1 as an output, followed by a new line and another input line. But if you want to use the same command without adding a new line, you need to type in additional commands after ‘echo’. Let’s go over it step by step:

How to write a newline with Echo in Bash shell?

And so there really isn't any general way to know how to write a newline with echo, except that you can generally rely on just doing echo to do so. A bash shell typically does not conform to the specification, and handles the -n and other options, but even that is uncertain.

How do I stop Echo in Bash?

Bash is the command console in Linux and Mac OS, which also recognizes the ‘echo’ command. In the case of Bash, echo also creates a new line in the output, but you can use different steps to stop it. The best way to remove the new line is to add ‘-n’. This signals not to add a new line.


3 Answers

Because the quotes are processed by the shell and the echo command receives plain -n. If you want to echo -n, you can e.g. printf '%s\n' -n

like image 158
laalto Avatar answered Oct 12 '22 15:10

laalto


I found that the following just works in bash:

echo -n PRINT_THIS

So, you should put -n the first place.

like image 42
Erik Bitemo Avatar answered Oct 12 '22 17:10

Erik Bitemo


You could try

echo -e '\055n'
like image 33
kennytm Avatar answered Oct 12 '22 17:10

kennytm