Learn by reading through in order

Formatted Output — printf

Unlike echo, printf adds no trailing newline, so you write \n yourself. Learn inserting values with %s/%d, aligning columns with \t, and creating multi-line files — hands-on in a browser terminal.

How printf Differs from echo — No Auto Newline

printf prints text just like echo, but unlike echo it doesn't add a trailing newline automatically.

You write \n yourself wherever you need a line break.

Since you can build the output exactly as you want, it's also good for creating multi-line files.

printf 'hello\n' prints hello and a newline.

Leave out \n and the next output continues on the same line.

Write it as printf 'a\nb\nc\n' > file and you create a 3-line file directly.

How echo and printf handle the newline
echo hellohello + newline (auto)printf 'hello\n'hello + newline (\n explicit)
echo adds a trailing newline automatically; printf breaks lines only where you write \n.
echo hello                       # hello + auto newline
printf 'hello\n'                 # hello + the newline you wrote
printf 'a\nb\nc\n' > letters.txt   # create a 3-line file
cat letters.txt                  # the 3 lines a / b / c
How to write itTrailing newlineResult
echo 'hi'Added automaticallyhi + newline
printf 'hi'Not addedjust hi (no newline)
printf 'hi\n'Not addedhi + newline because you wrote \n

① Create a 3-line file with printf 'a\nb\nc\n' > letters.txt.

② Confirm with cat letters.txt that a, b, c show on 3 lines.

③ Run printf 'no newline' and confirm there's no trailing newline, so the next prompt continues on the same line.

④ Run printf 'with newline\n' and confirm that writing \n adds a line break. (Run it correctly and an explanation will appear.)

Linux console
0 / 4 completed
Loading Linux Terminal...

Inserting Values and Aligning Columns — %s %d \t

Write %s (string) or %d (integer) in the format, and the values listed after it are inserted at those spots.

printf 'name=%s\n' alice prints name=alice.

It's better than simple concatenation with echo for fitting values into a fixed shape.

\t is a tab, used when you want to align columns.

The format is applied repeatedly, once for each set of values that follow.

Print a header and data each in a tab-separated format, like printf '%s\t%d\n' alice 30, and the columns line up.

Insert with %s, align columns with \t
alice into %sinsert a string (hi=alice)42 into %dinsert an integer (42)%s \t %salign columns with a tab
%s inserts a string, %d an integer, and \t aligns columns with a tab.
printf 'name=%s\n' alice        # name=alice
printf '%s\t%s\n' name age      # the header, tab-separated
printf '%s\t%d\n' alice 30      # a data row, aligned
SymbolMeaningExample → output
%sInsert a stringprintf '%s\n' hihi
%dInsert an integerprintf '%d\n' 4242
\tTab (column alignment)printf 'a\tb\n'a + tab + b
\nNewlineprintf 'x\n'x + newline

① Run printf 'name=%s\n' alice and confirm alice is inserted at the %s spot.

② Run printf '%s\t%s\n' name age and confirm \t lines up the two words tab-separated.

③ Then run printf '%s\t%d\n' alice 30 and confirm the header and data columns line up.

Linux console
0 / 3 completed
Loading Linux Terminal...
QUIZ

Knowledge Check

Answer each question one by one.

Q1How does printf differ from echo?

Q2What does printf 'name=%s\n' alice print?

Q3What does \t represent?