Bash – How to print out ā€œ-Eā€ in bash echo

bashecho

I want to execute echo and get an output like:

$ export EVAR="-E"
$ echo "$EVAR"
-E

I have stored "-E" in a bash variable, say $EVAR. If I execute echo $EVAR, echo will print out nothing. Perhaps it thinks $EVAR is an argument -E. Quoting $EVAR inside double quote marks doesn't work either.

How can I print it out?

NOTE: I imagine there could be a solution which is ignorant on the content of $EVAR – with no analysis on the content of $EVAR, a command like echo some-arg $EVAR. Is that possible? Or should I only turn to a workround like printf?

Best Answer

You could use tricks:

echo " $EVAR"
echo  -e "\055E"
echo $'\055E'

But as I said: those are tricks. The real solution is to use printf always:

$ printf '%s\n' "$EVAR"
-E
Related Question