The printf command in Linux: format and display text

Introduction

The commandprintfis an essential tool in the Linux command line that allows formatting and displaying text in a controlled way. Unlikeecho, printfIt interprets exhaust sequences and accepts format specifications similar to those of the C language, which makes it ideal to generate structured output in scripts and automations.

Basic syntax

The general form ofprintfis:

printf FORMATO [ARGUMENTOS...]

WhereFORMATOis a chain that can contain literal text and format specifiers; andARGUMENTOSare the values to be replaced in those specifications. If more arguments than specifiers are provided, the format is reused until they are all consumed.

Most used format Specifiers

  • %s- text chain.
  • %dor%i- whole decimal with sign.
  • %f- floating point number.
  • %xor%X- whole in hexadecimal (small / capital).
  • %o- whole octal.
  • %%- print the literal percentage sign.

These specifications can be accompanied by modifiers that control width, accuracy and alignment.

Simple examples

Show a greeting:

printf "Hola, %s\n" "Mundo"

Output:

Hola, Mundo

Show several numbers:

printf "%d %d %d\n" 10 20 30

Output:

10 20 30

Width and precision

You can specify a minimum width with a number between the%and the specific. For example,%5dreserve at least 5 characters, aligning to the right by default. To align to the left the minus sign is used:%-5s.

The accuracy for floating point numbers is indicated with a point followed by the amount of decimals:%.2fIt shows two decimals.

printf "Valor: %8.2f\n" 123.456

Output:

Valor:   123.46

Exhaust sequences

As in C,printfHe plays sequences like:

  • \n- line jump.
  • \t- horizontal tabulation.
  • \r- car return.
  • \\- literal inverted bar.
  • \"- double literal quotation.

These sequences allow you to create more complex designs without multiple calls.

Differences with echo

Althoughechois easier to print flat text, has a variable behavior regarding options-eand-naccording to the shell.printfis more portable and predictable, as it always interprets the exhaust sequences and does not automatically add a line jump to the end (unless included\nin the format). This makes it preferable in scripts where precise control of the output is required.

Good practices

  • Use simple quotes around the format when variable expansion is not needed, avoiding surprises.
  • Prefer%sfor chains and validate that the arguments match the specifiers to avoid format errors.
  • In loops, reuse the same format to maintain consistency and improve legibility.
  • When printing only text without format, considerprintf '%s\n' "texto"to avoid unexpected interpretation of exhaust sequences.

Conclusion

Domainprintfallows to generate formatted, aligned and numerical output in any Linux environment. Its syntax, inherited from C, provides flexibility that exceedsechoin most cases of professional use, from simple messages to the creation of automated tables and reports.

This work is under aCreative Commons License Attribution 4.0 International for Francesc Roig francesc @ vivaldi.net.

EnglishenEnglishEnglish