🐧 Linux / Shell Scripting
Beginner
How do echo and printf work in bash?
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.