I'm trying to assign -e to a variable in Bash 4. But the variable remains empty.
For example:
$ var="-e"
$ echo $var
$ var='-e'
$ echo $var
$ var="-t"
$ echo $var
-t
Why does it work with -t, but not -e?
It works, but running echo -e doesn't output anything in Bash unless both the posix and xpg_echo options are enabled, as the -e is then interpreted as an option:
$ help echo
echo: echo [-neE] [arg ...]
Write arguments to the standard output.
Display the ARGs, separated by a single space character and followed by a
newline, on the standard output.
Options:
-n do not append a newline
-e enable interpretation of the following backslash escapes
-E explicitly suppress interpretation of backslash escapes
Use
printf "%s\n" "$var"
instead.
And as cas notes in a comment, Bash's declare -p var (typeset -p var in ksh, zsh and yash (and bash)) can be used to tell the exact type and contents of a variable.
-e, they can run declare -p var.
A command (like your echo) takes command line arguments which could be flags (-e in your case). Many commands (at least common Linux versions) understand -- (two hyphens) as "end of flags, whatever follows is regular arguments". So you can delete a file perversely named -r by rm -- -r.
For displaying stuff, printf is more robust all around (if harder to use).
echowould output nothing, but the variable has the value-e(which is a valid option forechoinbash). You'd have the same issue with-nand-Eand combinations thereof.