Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
572 views
in Technique[技术] by (71.8m points)

escaping - How to echo the literal string "-e" (and nothing else) in bash?

How can I echo the literal string -e and nothing else?

I'm trying to better understand how shell arguments are escaped.

The following commands do not work:

echo -e # prints nothing
echo '-e' # prints nothing
echo "-e" # prints nothing
echo -e # prints nothing
echo \-e # prints -e
echo '-e' # prints -e
echo "'-e'" # prints '-e' (with quotes)
echo -- -e # prints -- -e

I can't find one that doesn't either include quotes or a leading slash.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

How to echo the literal string "-e" (and nothing else) in bash?

printf '%s
' '-e'
printf -- '-e
'

From man echo -e is an option:

-e
enable interpretation of backslash escapes

From bash builtins:

echo ...
If the -e option is given, interpretation of the following backslash-escaped characters is enabled.
...
echo does not interpret -- to mean the end of options.

So echo -e will make echo interpret -e as a flag and print empty newline. To print -e you basically have to use printf (or you can use an implementation of echo that will allow to do that). I don't believe it is possible to print only -e with bash echo builtin.

printf is more portable. There are implementations of echo which do not take -e argument and it may work. On the net you can find various sites about echo portability issues.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...