سؤال

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

echo \-; # Output:-
echo '-n'; #Output: nothing!
هل كانت مفيدة؟

المحلول

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.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top