Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
603 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 Reply

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...