Skip to main content
1 of 10
AdminBee
  • 23.6k
  • 25
  • 55
  • 77

The reason that it works correctly in your example is that

  • you have provided only one argument to echo, and
  • you have correctly quoted your variable That means that echo sees only one string to print, which is -n Hello. Had you used
echo $var 

without quotes instead, then $var would have been split at the space, and echo would have seen two arguments: -n (which it will interpret as an option), and Hello.

This means that correctly quoting your shell variables is one remedy against involuntarily running into a situation where echo (or in fact any other command) interprets an operand as an option.

However, consider e.g. a case where you have collected an array of string that you want echo to print, and one of the strings happens to also be an option argument to echo:

arguments=( "-n" "Hello" ) 

Even if you then correctly quote the variable, as in

echo "${arguments[@]}" 

the first token echo sees will still be -n which it interprets as option, rather than an operand.

TL/DR

The key point is the term "absolutely" in the source you quoted. In most cases, echo will be fine. But if you start relying on that blindly, there will at some point be the situation in which a program you call inadvertantly mis-interprets something you indended to be an "operand" parameter with an "option" argument.

AdminBee
  • 23.6k
  • 25
  • 55
  • 77