Skip to main content
2 of 3
Rephrase title, add relevant tags
AdminBee
  • 23.6k
  • 25
  • 55
  • 77

Why is it recommended to use "printf -- <arguments>" instead of "echo <arguments>?"

In fact, the echo command cannot be used with absolute safety here. If the variable contains -n for example, echo will consider that an option, rather than data to be printed. The only absolutely sure way to print the value of a variable is using print: printf "%s\n" "$foo" source

When I type:

var="-n Hello" echo "$var" 

It works properly and doesn't interpret -n as an option, so I don't understand why the source I quoted says that we need to use printf to be absolutely safe?