views:

235

answers:

5

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

A: 

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.

Pointy
Doesn't work for me. Output `- -n`
extraneon
For me that does this> echo - -n- -n
Greg Reynolds
Hmm, well as I just noted in an edit, it probably varies from shell to shell.
Pointy
Sorry I didn't get you.Can you explain your point please?
Happy Mittal
Linux/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
+4  A: 

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

laalto
it's a shame `--` (end of options) isn't supported :(
extraneon
A: 

you should try to use the more portable printf as far as possible.

ghostdog74
+1  A: 

You could try

echo -e '\055n'
KennyTM
A: 

Not quiete there:

echo -e 'a-n\b\b\b '
 -n

surprisingly this works:

echo -e '-n\b'
-n

and here a solution I can explain:

echo "foobar" | sed 's/.*/-n/'
-n
user unknown