Sunday, October 24, 2021

[SOLVED] Bash: echo string that starts with "-"

Issue

VAR="-e xyz"
echo $VAR

This prints xyz, for some reason. I don't seem to be able to find a way to get a string to start with -e.

What is going on here?


Solution

The variable VAR contains -e xyz, if you access the variable via $ the -e is interpreted as a command-line option for echo. Note that the content of $VAR is not wrapped into "" automatically.

Use echo "$VAR" to fix your problem.



Answered By - heb