Nice one!
What is happening is the first -e
is being interpreted as an option for echo (to enable escape sequences'
Usually, you'd do something like echo -- "-e"
, and it should print simply -e
, but echo is happy to behave differently, and simply prints out -- -e
as a whole string.
echo does not interpret -- to mean the end of options.
The solution to the problem could also be found in the man pages:
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.
So something like this should work:
x=(-a 2);echo "${x[@]}";x=(-e 2 -e); env echo "${x[@]}"