printf and echo
Use printf format arguments... for predictable formatting. %s inserts text, %d formats an integer, and a width such as %-10s pads a string on the right to at least ten columns for simple ASCII text. \n in the format creates a newline. Bash repeats the format when more arguments remain. echo is convenient for fixed plain messages, but the treatment of -n and backslash escapes varies across implementations and settings (including Bash xpg_echo). For arbitrary text prefer printf '%s\n' "$value". The formatting examples here use small trusted integer literals; validate external numeric inputs before numeric conversion.
Practical examples
Run each example separately in Bash 5.2 in a fresh empty practice directory, using default shell options and C locale. Output is shown separately; do not enter it as code.
Literal text
value='-n'printf '%s\n' "$value"value='100% ready'printf '%s\n' "$value"Expected stdout:
-n100% readyExpected stderr:
No output
Overall example exit status: 0
Neither option-like text nor percent signs are interpreted as formatting instructions.
Two columns
Expected stdout:
apples 4pears 12Expected stderr:
No output
Overall example exit status: 0
The fixed format is reused for each pair. The numeric field is right-aligned.
Compare newline behavior
Expected stdout:
ReadyABExpected stderr:
No output
Overall example exit status: 0
With no newline in the second format, A and B appear on the same line.
Common mistakes
Do not put arbitrary text in the format position. printf "$value" can interpret percent signs or escapes. echo "$value" can also interpret option-like text; quoting protects argument boundaries, not the called command’s option rules.
Practice
Print a heading and two rows with names and integer counts using one repeated fixed format. Also print the literal string -n on its own line.
Next: Read input with read -r