How do I echo “-e”?

2019-01-20 11:43发布

问题:

I want to echo a string that might contain the same parameters as echo. How can I do it without modifying the string?

For instance:

$ var="-e something"
$ echo $var
something

... didn't print -e

回答1:

A surprisingly deep question. Since you tagged bash, I'll assume you mean bash's internal echo command, though the GNU coreutils' standalone echo command probably works similarly enough.

The gist of it is: if you really need to use echo (which would be surprising, but that's the way the question is written by now), it all depends on what exactly your string can contain.

The easy case: -e plus non-empty string

In that case, all you need to do is quote the variable before passing it to echo.

$ var="-e something"
$ echo "$var"
-e something

If the string isn't eaxctly an echo option or combination, which includes any non-option suffix, it won't be recognized as such by echo and will be printed out.

Harder: string can be -e only

If your case can reduce to just "-e", it gets trickier. One way to do it would be:

$ echo -e '\055e'
-e

(escaping the dash so it doesn't get interpreted as an option but as on octal sequence)

That's rewriting the string. It can be done automatically and non-destructively, so it feels acceptable:

$ var="-e something"
$ echo -e ${var/#-/\\055}
-e something

You noticed I'm actually using the -e option to interpret an octal sequence, so it won't work if you intended to echo -E. It will work for other options, though.

The right way

Seriously, you're not restricted to echo, are you?

printf '%s\n' "$var"


回答2:

The proper bash way is to use printf:

printf "%s\n" "$var"

By the way, your echo didn't work because when you run:

var="-e something"
echo $var

(without quoting $var), echo will see two arguments: -e and something. Because when echo meets -e as its first argument, it considers it's an option (this is also true for -n and -E), and so processes it as such. If you had quoted var, as shown in other answers, it would have worked.



回答3:

Quote it:

$ var="-e something"
$ echo "$var"
-e something

If what you want is to get echo -e's behaviour (enable interpretation of backslash escapes), then you have to leave the $var reference without quotes:

$ var="hi\nho"
$ echo $var
hi
ho

Or use eval:

$ var="hi\nho"
$ eval echo \${var}
hi\nho

$ var="-e hi\nho"
$ eval echo \${var}
hi
ho


回答4:

Since we're using bash, another alternative to echo is to simply cat a "here string":

$ var="-e something"
$ cat <<< "$var"
-e something
$ var="-e"
$ cat <<< "$var"
-e
$ 

printf-based solutions will almost certainly be more portable though.



回答5:

Try the following:

$ env POSIXLY_CORRECT=1 echo -e
-e

Due to shell aliases and built-in echo command, using an unadorned echo interactively or in a script may get you different functionality than that described here. Invoke it via env (i.e., env echo ...) to avoid interference from the shell.

The environment variable POSIXLY_CORRECT was introduced to allow the user to force the standards-compliant behaviour. See: POSIX at Wikipedia.

Or use printf:

$ printf '%s\n' "$var"

Source: Why is bash swallowing -e in the front of an array at stackoverflow SE



回答6:

Use printf instead:

var="-e bla"
printf "%s\n" "$var"

Using just echo "$var" will still fail if var contains just a -e or similar. If you need to be able to print that as well, use printf.



标签: string bash echo