Domanda

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

echo \-; # Output:-
echo '-n'; #Output: nothing!
È stato utile?

Soluzione

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.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top