Skip to content

printf Linux Command Guide

The printf command in Linux is a powerful tool for formatting and printing text. It allows users to control the output format by specifying placeholders and format specifiers. By utilizing various options and flags, users can customize the output to meet their specific needs. Whether you are looking to display text in a particular way or generate complex output, mastering the printf command can greatly enhance your command-line productivity.

printf Syntax:

Terminal window
printf [option] format [argument]...

Options:

OptionDescription
-vuse the value of variable VAR
mark the end of options and disable further option processing

Parameters:

ParameterDescription
formatformat string controlling the output
argumentarguments to be formatted and printed

Display Text

Terminal window
printf "Hello, World!\n"

Prints the text “Hello, World!” followed by a newline character.

Format Numeric Output

Terminal window
num=25
printf "The number is: %d\n" $num

Formats and prints the numeric variable with a specified format (%d for integers).

Display Float Number

Terminal window
float_num=3.14159
printf "The value of pi is: %.2f\n" $float_num

Prints the floating-point number with two decimal places.

Padding Output with Zeros

Terminal window
num=7
printf "Padded number is: %05d\n" $num

Adds leading zeros to a number to make it 5 digits long.

Output Multiple Variables

Terminal window
name="Alice"
age=30
printf "Name: %s, Age: %d\n" $name $age

Prints multiple variables with different formats in a single printf statement.

Formatting Strings

Terminal window
text="Linux"
printf "Title: %-10s Rocks!\n" $text

Formats and prints a string with a left-justified width of 10 characters.

Display Special Characters

Terminal window
printf "This is a backslash: \\\n"

Displays a backslash character within the printf statement.

printf FAQ:

How do I use printf in Linux?

To use the printf command in Linux, execute the following command:

Terminal window
printf "Hello, World!\n"

What are the common formatting options in printf?

The common formatting options in printf include placeholders for variables like %s for strings, %d for integers, %f for floating-point numbers, and so on, along with escape sequences for special characters.

Terminal window
printf "Welcome, %s! Your balance is $%d.\n" "John" 1000

How can I align text using printf in Linux?

You can align text using printf in Linux by specifying the field width along with alignment flags. For example, to right-align text in a field of width 10, you can use the following:

Terminal window
printf "%10s\n" "Right-aligned"

How do I format floating-point numbers with printf in Linux?

To format floating-point numbers with printf in Linux, specify the precision and width along with the %f placeholder. For example, to display a floating-point number with 2 decimal places:

Terminal window
printf "Pi is approximately %.2f\n" 3.14159

How can I print a tab or newline character with printf in Linux?

To print a tab character or newline character with printf in Linux, you can use the escape sequences \t for tab and \n for newline. For example, to print “Hello” followed by a tab and “World” on a new line:

Terminal window
printf "Hello\tWorld\n"

How do I redirect the output of printf to a file in Linux?

You can redirect the output of printf to a file in Linux using the redirection operator >. For example, to write “Output” to a file named “file.txt”:

Terminal window
printf "Output" > file.txt

Applications of the printf command

  • Format and print output in a specific way
  • Insert variables and values in a formatted string
  • Display text with specified padding, justification, and width
  • Construct complex output with control over formatting