Why doesn't $echo '-n'
write -n
on terminal although -n
is written within single quotes?
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:
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:
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.
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.
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
I found that the following just works in bash:
echo -n PRINT_THIS
So, you should put -n the first place.
You could try
echo -e '\055n'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With