The printf command in Linux: A Complete Guide

Printf Command In Linux

The printf command is one of the basic commands that are very popular among Linux enthusiasts and users. It allows one to print formatted output. In their article, we will be covering all, from basics to advanced uses for the printf command. So stick around for a bit, and you will get to learn a few tips and tricks.

Also read: Echo command in Linux

Basic use of the printf command

Every Linux command can be divided into three parts, the command, the options or flags available and the argument(s). The printf command has two options and usually, the argument is a line of text, which is preceded by a format specification that the user wants.

The first option is the usual –help one that prints the help screen onto the terminal or the standard output, and the next and the final option is the -v flag, which (may not be present in all distributions/versions) creates a variable and stores the line of string into it, instead of sending the data to standard output.

Running the following code shows the user the current version of the printf command:

type -a printf
Output showing the different versions of printf available
Output showing the different versions of printf available

Use string and new-line formatting inside printf

Any user can use the print command and incorporate string formatting and new-line character as they wish. The following produces a full, meaningful sentence, even though the argument inside the inverted quotes is not one, (though we provide the strings that are replaced into the %s symbols), the user then adds the \n symbol signifying a new line.

printf "I like %s and %s for breakfast\n" eggs toast

Use integer and float replacements

The user can very easily replace the following symbols by giving the values are arguments. The %d is for decimals, %f is for floating point numbers. The following example illustrates the situation: try to run the code yourself and see the results!

printf "%s %d %f" 10 10 10
Terminal output shows the different formatting
The terminal output shows the different formatting

Sequences for different types of formatting

The user can use many different kinds of formatting, as we have seen before the \n character signifies a new line to be started. Similarly, all of the below sequences stand for different formatting options, see the below chart for reference:

  • \” double quote
  • \\ backslash
  • \a alert
  • \b backspace
  • \c produce no further output
  • \e escape
  • \f form feed
  • \n new line
  • \r carriage return
  • \t horizontal tab
  • \v vertical tab

Echo and the Printf command which one to use?

So as you may have known, the echo command is another command that can display text to screen in Linux. But which one to use when? The answer lies in the difference between the two commands. The echo command creates a new line every time it is executed automatically, basically the \n symbol we learnt about is built right into the echo command. So that is one advantage to use the echo command.

But the echo command does not provide the plethora of formatting options we get while using the print command. We cannot use tabs, carriage returns, string or integer formatting while using the echo command. So because of the ease of formatting, we extensively use the print command in scripts, while the echo command is used more in terminal instances while working on something else and we just want to echo something to the screen.


We hope that you readers have found some value in this basic tutorial, don’t forget to keep learning the basics and keep brushing up on these skills time and again. For more such Linux-related tutorials and articles don’t forget to follow LinuxForDevices! That’s it for this article and as always thanks for reading!