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

- 30,738
- 21
- 105
- 131

- 3,667
- 12
- 44
- 60
-
What language you using? – thecoshman Apr 15 '10 at 12:33
-
I guessed that this is a shell syntax question (ie, bash or whatever). – Pointy Apr 15 '10 at 12:35
-
@tripleee - I'm not sure this question is a dup of the cited question. The question in the body seems to be different than the titular digest. Could you take a second look at it? – jww Apr 25 '18 at 23:13
-
https://ss64.com/bash/echo.html – JJFord3 Jan 08 '19 at 15:59
-
Isn't the duplicate for the ***opposite*** situation (unintentional vs. intentional)? – Peter Mortensen Nov 05 '21 at 15:05
6 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

- 150,114
- 66
- 286
- 303
I found that the following just works in bash:
echo -n PRINT_THIS
So, you should put -n the first place.

- 59
- 1
You should try to use the more portable printf
as far as possible.

- 30,738
- 21
- 105
- 131

- 327,991
- 56
- 259
- 343
-
Can you provide an example in your answer? (But ***without*** "Edit:", "Update:", or similar - the answer should appear as if it was written today.) – Peter Mortensen Nov 05 '21 at 16:49
The quotes don't help because ... ugh, it's hard to explain. The shell strips the quotes off before the "echo" command itself is evaluated, so by that time they don't matter.
Try this:
echo - -n
That's not documented as working (I'm running Ubuntu Linux), but echo
is almost certainly a built-in to whatever shell you're using, so the man page is questionable anyway. It does work for me. (I'm running zsh
because I'm really suave and sophisticated).
edit: well it seems that the bash
builtin edit
doesn't behave that way. I don't know what to suggest; this:
echo '' -n
will give you "-n" preceded by a space, which (depending on what you're doing) might be OK.

- 405,095
- 59
- 585
- 614
-
-
-
Hmm, well as I just noted in an edit, it probably varies from shell to shell. – Pointy Apr 15 '10 at 12:37
-
-
1Linux/UNIX systems support multiple command line interpreters (shells). The `echo` command is usually built in to each of those, and the exact syntax and features can vary. You're probably using the default shell, `bash`, which doesn't work like my shell (`zsh`) does. – Pointy Apr 15 '10 at 12:41
-
Not quite there:
echo -e 'a-n\b\b\b '
-n
Surprisingly, this works:
echo -e '-n\b'
-n
And here is a solution I can explain:
echo "foobar" | sed 's/.*/-n/'
-n

- 30,738
- 21
- 105
- 131

- 35,537
- 11
- 75
- 121
-
Re *"here is a solution I can explain"*: Can you reveal the explanation? – Peter Mortensen Nov 05 '21 at 14:56
-