Using echo
and printf
for Output
Displaying information to users is a fundamental aspect of writing shell scripts. Two of the most commonly used commands for output are echo
and printf
. Each has its unique features and best-use scenarios. This post explores both commands, their syntax, differences, and practical tips for utilizing them to their fullest.
Why Output Matters in Scripts
Output informs users about a script’s progress, results, and issues. Clean and properly formatted output makes scripts easier to interact with, debug, and maintain. Whether you’re prompting a user for input, logging operations, or displaying error messages, knowing how to control your script’s output is essential.
The echo
Command
Basic Usage
echo
is the simplest way to print text to the terminal.
echo "Hello, world!"
Output:
Hello, world!
Variables and Command Substitution
echo
works seamlessly with variables and command substitution:
name="Ada"
echo "Hello, $name!"
echo "Today is: $(date)"
Handling Special Characters
By default, echo
interprets escape characters literally:
echo "First Line\nSecond Line"
Output:
First Line\nSecond Line
To process escape sequences like \n
for new lines, use the -e
option:
echo -e "First Line\nSecond Line"
Output:
First Line
Second Line
No Trailing Newline
Suppress the trailing newline with -n
:
echo -n "No newline at the end"
The printf
Command
What Makes printf
Different?
printf
is more powerful and flexible than echo
, offering C-like formatting control over your output. It’s ideal for producing structured or tabular output or handling edge cases where echo
is inconsistent.
Basic Usage
Syntax: printf FORMAT [ARGUMENTS...]
printf "Hello, world!\n"
Note: Unlike echo
, printf
does not automatically add a newline.
Formatting Variables
You can specify format specifiers for types and width:
num=7
word="apples"
printf "I have %d %s.\n" "$num" "$word"
Output:
I have 7 apples.
Width and Padding
Use %Ns
for strings with width, %0Nd
for padded numbers:
printf "|%10s|\n" "foo"
printf "%05d\n" 8
Output:
| foo|
00008
Tabular Output
Create columns by specifying widths:
printf "%-10s %5s\n" "Item" "Count"
printf "%-10s %5d\n" "Apple" 10
printf "%-10s %5d\n" "Orange" 120
Output:
Item Count
Apple 10
Orange 120
echo
vs. printf
: When to Use Which?
- Use
echo
for quick, uncomplicated output—plain lines, short script messages, simple status updates. - Use
printf
when you need:- Formatting like columns or padded numbers
- Precise control of special characters
- Consistent behavior across systems
Practical Tips
- Always quote variables to prevent word splitting:
echo "$var"
,printf "%s\n" "$var"
- Prefer
printf
for portability in complex scripts (someecho
features vary by shell). - Remember to add
\n
to the format string inprintf
to insert newlines where needed.
Conclusion
Displaying output in shell scripts is more than just printing text—it’s about communicating clearly and precisely. Use echo
for simple output, and reach for printf
when you need advanced formatting or cross-shell reliability. With these commands in your toolkit, your scripts will be more robust, readable, and user friendly.
Summary:
Display outputs in scripts with formatting.