How do echo and printf work in bash?

Why Interviewers Ask This

Foundational questions like this help interviewers calibrate the rest of the interview. A confident, accurate answer signals that you have solid Linux / Shell Scripting basics — a prerequisite for any developer role.

Answer

echo text prints text to stdout followed by a newline. Use echo -n to suppress the trailing newline and echo -e to interpret escape sequences like \n (newline) and \t (tab). printf is more powerful and portable — it uses C-style format strings: printf "Name: %s, Age: %d\n" "Alice" 30. printf does not add a newline automatically. printf is preferred in scripts because echo's behavior with options and escape sequences varies between shells and implementations. Always quote variables in echo to prevent word splitting: echo "$var" not echo $var.

Pro Tip

This topic has Linux / Shell Scripting-specific nuances that differ from general programming. Highlighting those nuances in your answer shows expertise rather than generic knowledge.