Read

Module 3 · Text, redirection and pipes

echo and printf

The two commands that put text on the screen: echo for quick messages, printf when the format has to be exact.

What you will learn

  • Print text and variables with echo, with and without a trailing newline.
  • Use printf with %s and %d placeholders and escape sequences like \n and \t.
  • Know when quoting matters and why printf is the portable choice.

Almost everything in this module is about moving text around: into files, through pipes, between commands. Before you can move text you need to produce some, and the two tools for that are echo and printf. They look trivial, and echo mostly is, but the details decide whether a script works everywhere or only on your machine.

echo: say it and add a newline

echo prints its arguments separated by single spaces and ends with a newline. Because the shell splits arguments on whitespace, echo a b prints a b: the extra spaces vanished before echo ever saw them. Quote the text to keep it exactly as typed. Variables expand inside double quotes, so echo "Home is $HOME" prints the path; inside single quotes nothing expands.

Two options are worth knowing. -n suppresses the final newline, which is handy for prompts. -e turns on escape sequences such as \n (newline) and \t (tab). In this VM echo is a builtin of the ash shell and supports both; on other systems -e may be missing or the escapes may be interpreted without asking. That inconsistency is the main reason printf exists.

printf: a format, then the values

printf FORMAT [ARGS...] works like the C function of the same name. The first argument is a template; each %s is replaced by the next argument as a string, each %d by the next argument as an integer. Escape sequences in the format are always interpreted, so \n is a newline on every Unix. Crucially, printf does not add a newline by itself: if you want one, write \n at the end.

Width and alignment live between the % and the letter: %6s right-aligns in six columns, %-6s left-aligns, %05d pads a number with zeros. If you pass more arguments than placeholders, the format is reused for the leftovers, which is a quick way to print a list one item per line.

~% echo Hello, world
Hello, world
~% echo -n "no newline"; echo " <- same line"
no newline <- same line
~% echo "User: $USER"
User: root
~% echo 'User: $USER'
User: $USER
~% printf '%s has %d items\n' basket 3
basket has 3 items
~% printf '%-6s|%4d|\n' ab 42
ab    |  42|
~% printf '%s\n' one two three
one
two
three

Which one to use

SituationUse
A quick message or a variable's valueecho
Tabs, newlines or anything with a backslashprintf
Aligned columns or zero-padded numbersprintf with widths
Output without a trailing newlineecho -n or printf without \n

In the exercises you will write what echo and printf print into files using > file. That arrow sends a command's output into a file instead of the screen; the next lesson explains it properly, but you only need the shape for now.

Commands in this lesson

CommandWhat it does
echo TEXTPrint TEXT followed by a newline.
echo -n TEXTPrint TEXT without the trailing newline.
echo -e 'a\tb'Interpret escapes such as \t and \n (shell-dependent).
printf 'FORMAT\n' ARGSPrint ARGS according to FORMAT; %s string, %d integer.
printf '%-10s|%5d|\n' name 42Left-align in 10 columns, right-align a number in 5.
printf '%s\n' a b cReuse the format: one argument per line.

Quiz

  1. What does `echo a b` print?

    • `a b`, spaces preserved
    • `a b`, because the shell split the arguments
    • An error: too many arguments
  2. Which command prints `done` without moving to a new line afterwards?

    • `echo -e done`
    • `echo -n done`
    • `printf 'done\n'`
  3. What does `printf '%d-%s\n' 7 up` print?

    • `7-up`
    • `%d-%s 7 up`
    • `up-7`
    • Nothing: printf needs -e
  4. Why is `printf` preferred over `echo -e` in scripts?

    • It is faster
    • It behaves the same on every Unix, while echo's handling of -e and escapes varies
    • echo cannot print variables
  5. What does `printf '%s\n' a b` print?

    • `a b` on one line
    • `a` on one line and `b` on the next
    • Only `a`; `b` is ignored

Practice

  1. In /root/lab/l21, use echo to create a file greeting.txt whose only line is exactly `Hello, lab!` (write it with `echo ... > /root/lab/l21/greeting.txt`).

  2. In /root/lab/l21, use a single printf with a reused format to create table.txt containing three lines: `1 one`, `2 two` and `3 three`.

  3. Print the value of your HOME variable on the screen with echo.

Open this lesson in the app to do the tasks in a real Linux machine and have them checked.