Interview Prep/Shell Scripting/Variables, quoting and expansions

What is the difference between `echo` and `printf`, and why do production scripts prefer `printf`?

IntermediateConceptVariables, quoting and expansions
Answer

echo behaviour varies: some shells interpret \n by default, -e/-n are not portable, and a value like -n or -e is swallowed as an option. printf is consistent everywhere and supports formatting.

printf '%s\n' "$user_input"           # always prints the value literally
printf '%-20s %5d%%\n' "/var" 93       # aligned columns
printf -v stamp '%(%Y-%m-%dT%H:%M:%S)T' -1   # timestamp, no fork (Bash 4.2+)

Rule: echo for fixed strings, printf '%s\n' for anything containing variables.

All Shell Scripting questions Roadmap: Shell scripting (Bash)

More on Variables, quoting and expansions