printf Command in Linux



The printf command in Linux formats the output. It precisely controls text formatting, including spacing, alignment, and number formatting. It is more powerful and flexible than echo, making it useful for structured output, formatting numbers, aligning text, and handling special characters.

Table of Contents

Here is a comprehensive guide to the options available with the printf command −

Syntax of printf Command

The syntax of the printf command in Linux is as follows −

printf [format] [arguments]

In the above syntax, the [format] field is used to specify a string, which defines the output structure. It contains −

  • Plain text − Directly printed as is.
  • Format specifiers − Marked by %, controlling how arguments are displayed.
  • Escape sequences − Special characters like \n for newline.

The [arguments] is an optional field to specify the values that replace format specifiers in the format string.

Format Specifiers for printf Command

The format specifiers for the printf command are listed below −

Specifier Type Description
%s String Formats and prints a string.
%d Integer (Decimal) Prints an integer in decimal format.
%i Integer Same as %d, used for decimal integer values.
%f Floating-Point Prints a floating-point number with six decimal places by default.
%e Scientific Notation Prints a floating-point number in scientific notation (lowercase e).
%E Scientific Notation Prints a floating-point number in scientific notation (uppercase E).
%g Shortest Representation Uses either %f or %e, whichever is shorter (lowercase).
%G Shortest Representation Uses either %f or %E, whichever is shorter (uppercase).
%x Hexadecimal (Lowercase) Prints an integer in a hexadecimal format (lowercase).
%X Hexadecimal (Uppercase) Prints an integer in hexadecimal format (uppercase).
%o Octal Prints an integer in octal format.
%c Character Prints the arguments as a single character.
%% Literal Prints a percent sign (%).

The modifiers are used to modify the format specifiers. They are listed below −

Modifier Type Description
%Ns String Right-aligns a string within N spaces.
%-Ns String Left-aligns a string within N spaces.
%0Nd Integer Prints an integer padded with leading zeros (N characters wide).
%+d Integer Forces the sign (+ or -) for numeric values.
%.Nf Floating-Point Limits a floating-point number to N decimal places.

Examples of printf Command in Linux

This section explains how to use the printf command in Linux with examples −

  • Printing a Simple String
  • Printing a String using Format Specifiers
  • Formatting the Floating-Point Number
  • Printing a Floating-Point Number in Scientific Notation
  • Zero-Padding a Number
  • Aligning a String
  • Printing a Number in Hexadecimal and Octal Formats
  • Printing the Percentage Sign
  • Printing and Formatting Date and Time
  • Displaying Usage Help

Printing a Simple String

To print a simple string, use the printf command in the following way −

printf 'Welcome to Tutorialspoint!\n'
printf Command in Linux1

Note that the Bash interprets ! as a history expansion character. To prevent the expansion, use single quotes.

Printing a String using Format Specifiers

The format specifiers are used to format the output. For example, the following command formats and prints a string by replacing %s with "Sam" and %d with 20, then \n adds a new line −

printf "Name: %s, Age: %d\n" "Sam" 20
printf Command in Linux2

Formatting the Floating-Point Number

To format the floating point number, %.nf specifier is used with the printf command. The n indicates the decimal places −

printf "Number: %.2f\n" 1.2345678
printf Command in Linux3

The %f specifier rounds floating-point numbers to six decimal places by default −

printf "Number: %f\n" 1.2345678
printf Command in Linux4

Printing a Floating-Point Number in Scientific Notation

To print the floating-point number in scientific notation, use the %e and %E specifiers for lowercase and uppercase E, respectively −

printf "%e\n" 12345.6789
printf Command in Linux5

Similarly −

printf "%E\n" 12345.6789
printf Command in Linux6

Zero-Padding a Number

To pad zeros to a number, a modifier is used with the %d specifier. For example, to make a number four characters long padded with leading zeros, use the following command −

printf "Number: %04d\n" 12
printf Command in Linux7

Aligning a String

To align a string, the %Ns and %-Ns modifiers are used, where N is the number of spaces to add on either side of the string −

printf "|%10s:%-10s|\n" "Right" "Left"
printf Command in Linux8

Printing a Number in Hexadecimal and Octal Formats

To print a number in hexadecimal and octal format, the %x and %o specifiers are used −

printf "Hex: %x, Octal: %o\n" 170 170
printf Command in Linux9

To print a number in uppercase hexadecimal, use the %X specifier −

printf "Hex: %X\n" 170
printf Command in Linux10

Printing the Percentage Sign

The percentage sign (%) is used with specifiers. To print it, use it with the printf command in the following way −

printf "Progress: 20%% complete\n"
printf Command in Linux11

Printing and Formatting Date and Time

To print and format the date and time, use the printf command in the following way −

printf "Date: %s\nTime: %s\n" "$(date +"%d-%m-%Y")" "$(date +"%H:%M:%S")"
printf Command in Linux12

Displaying Usage Help

To display the usage help about the printf, execute the following command −

help printf

Conclusion

The printf command in Linux provides precise control over text formatting, making it more powerful than echo. It supports format specifiers, escape sequences, and modifiers to structure output, align text, format numbers, and handle special characters.

Various examples demonstrate its versatility, including printing strings, formatting floating-point numbers, aligning text, padding numbers, and displaying data in different numerical bases.

Advertisements