Newline Echo Cancellation

Why
$ echo '-n'
doesn't write -n to the terminal, although -n is written in quotation marks?

+6
scripting bash newline echo
source share
6 answers

Since quotation marks are processed by the shell, and the echo command receives the usual -n . If you want an echo -n , you can, for example, printf '%s\n' -n

+8
source share

you should try to use more portable printf as much as possible.

+2
source share

You can try

 echo -e '\055n' 
+2
source share

I found that in bash only the following works:

 echo -n PRINT_THIS 

So you should put -n in first place.

+2
source share

The quotes do not help, because ... ugh, this is hard to explain. The shell removes the quotation marks before the echo command itself is evaluated, so by that time they do not matter.

Try the following:

 echo - -n 

This is not documented as work (I am running Ubuntu Linux), but echo almost certainly embedded in any shell that you use, so anyway the man page is questionable. This works for me. (I run zsh because I'm really smart and sophisticated).

edit : it seems that bash builtin edit not behaving like this. I do not know what to offer; this is:

 echo '' -n 

will give you a β€œ-n”, preceded by a space, which (depending on what you do) might be ok.

0
source share

Not quieter:

 echo -e 'an\b\b\b ' -n 

surprisingly this works:

 echo -e '-n\b' -n 

and here is a solution that I can explain:

 echo "foobar" | sed 's/.*/-n/' -n 
0
source share

All Articles