echo & printf
echo prints its arguments followed by a newline. It is the simplest way to show messages, values of variables and to write short text into files. Its more powerful sibling printf gives you exact control over formatting.
echo basics
echo Hello, Linux
echo "Two spaces kept"
echo
echo "The line above is empty"
city="Lisbon"
echo "I live in $city"
echo 'Not expanded: $city'Hello, Linux
Two spaces kept
The line above is empty
I live in Lisbon
Not expanded: $city
echo with no arguments prints an empty line. Double quotes keep spacing and expand variables; single quotes print everything literally.
Options: -n and -e
| Option | Effect |
|---|---|
-n |
Don't print the trailing newline |
-e |
Interpret backslash escapes such as \n (newline) and \t (tab) |
-E |
Don't interpret escapes (the default in Bash) |
echo -n "Loading..."
echo " done"
echo "a\tb"
echo -e "a\tb"
echo -e "line 1\nline 2"Loading... done
a\tb
a b
line 1
line 2
Note:
echo's handling of options and escapes differs between shells (Bash,sh, zsh, macOS). That's why scripts that need exact output preferprintf, which behaves the same everywhere.
Writing to files
Redirect echo output with > (overwrite) or >> (append):
echo "milk" > shopping.txt
echo "bread" >> shopping.txt
echo "eggs" >> shopping.txt
cat shopping.txt
echo "coffee" > shopping.txt
echo "--- after > again:"
cat shopping.txtmilk
bread
eggs
--- after > again:
coffee
A single > replaces the file's content: an easy way to lose data by accident.
printf
printf FORMAT ARGUMENTS... works like printf in C. The format contains text plus conversion specifiers:
| Specifier | Prints |
|---|---|
%s |
A string |
%d |
An integer |
%f / %.2f |
A decimal number (with 2 decimals) |
%5d / %-10s |
Pad to width 5 (right-aligned) / 10 (left-aligned) |
%05d |
Pad with zeros |
%% |
A literal % |
\n, \t |
Newline, tab |
Unlike echo, printf does not add a newline automatically:
printf 'Hello, %s!\n' "Grace"
printf '%d items cost %.2f EUR\n' 3 7.5
printf '[%5d] [%-6s] [%05d]\n' 42 ok 7
printf 'Disk usage: %d%%\n' 83Hello, Grace!
3 items cost 7.50 EUR
[ 42] [ok ] [00007]
Disk usage: 83%
Format reuse: tables in one line
If you pass more arguments than the format uses, printf repeats the format until all arguments are consumed. That makes small tables easy:
printf '%-10s %6s\n' "Product" "Price"
printf '%-10s %6.2f\n' apples 2.5 bananas 1.25 cherries 12
printf '%s\n' red green blueProduct Price
apples 2.50
bananas 1.25
cherries 12.00
red
green
blue
printf '%s\n' ... is the reliable way to print each argument on its own line.
Saving formatted text in a variable
Bash's printf -v name stores the result in a variable instead of printing it:
printf -v invoice 'INV-%04d' 37
echo "Next invoice: $invoice"
printf -v line '%*s' 20 ''
echo "[${line// /-}]"Next invoice: INV-0037
[--------------------]
%*s takes the width from an argument (20), producing 20 spaces, which ${line// /-} then replaces with dashes: a common trick for drawing separator lines.
Common mistakes
echo "Total: $5"printsTotal:because$5is the fifth script argument. Escape it:\$5, or use single quotes.printf "$message"treats%signs inside the message as format codes. Always writeprintf '%s\n' "$message".
Test yourself
What does printf '%-5s|\n' ab print?
ab|ab |ab|-5ab|
Which command prints x and y on separate lines in Bash?
- echo 'x\ny'
- echo -n x y
- printf '%s\n' x y
- printf x y
What is the difference between > and >>?
- None
>appends,>>overwrites>overwrites the file,>>appends to it>>redirects errors