Pergunta

How to escape special character? I want to print minus character.

echo \-; # Output:-
echo '-n'; #Output: nothing!
Foi útil?

Solução

You are attempting to print -n which is interpreted as an argument to disable printing of the trailing newline.

Here printf comes handy:

$ printf "%s" "-n"
-n

If you want a newline after n,

$ printf "%s\n" "-n"
-n

An ugly way using echo would be to use the octal value for the hyphen, i.e. -,

$ echo -e '\055n'
-n

The -e argument enables interpretation of backslash escapes.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top