Skip to main content

Linux command basics: printf

Use printf to format text or numbers.
Image
type writer

Photo by Dominika Roseclay from Pexels

Do you remember learning about root words in your language classes? For English speakers, like myself, much of our language is comprised of Greek and Latin roots, with added prefixes and suffixes to create new words. Similarly, commands used across multiple languages routinely have a common naming convention and may even perform like tasks.

The printf command traces its roots back to the development world but offers practical utility to the sysadmin, as well. Derived from the print function in C programming languages, it provides the user with the ability to print a formatted string of output. It works on text, numerical output, or a combination of the two. This can be powerful when paired with a variable.

The syntax

The command syntax of printf is pretty straightforward according to the man page, however, it can become confusing quickly. The basic syntax is as follows:

printf FORMAT [ARGUMENT]...
printf OPTION

In the first example, FORMAT alters the output in the same manner that it would in the C languages.

Where it gets weird...

There are three types of objects described here: Standard characters, interpreted characters, and conversion specifications. Let's look at all three below:

1. Standard characters inserted directly into the output.

2. Interpreted characters marked by \ (seen below):

Common interpreted characters:

\" double quote
\\ backslash
\b backspace
\c produce no further output
\e escape
\n new line
\t horizontal tab
\v vertical tab

3. Conversion specifications (changes how an argument is translated into the output).

Each conversion specification is noted by the % and ends with a conversion character (seen below):

d, i An integer (expressed as a decimal)
u An integer (expressed as an unsigned decimal)
x, X An integer (expressed as an unsigned hexadecimal)
o An integer (expressed as an unsigned octal)
s A string
c An integer (expressed as ASCII)

There are others, however, these are some of the most common.

Next, let's look at usage.

[ You might also like: 11 Linux commands I can’t live without ]

Basic usage

The most basic use cases for printf is outputting strings of text without receiving an error.

Seen here:

[tcarrigan@client ~]$ printf "Hello World."
Hello World.

If you wanted to get a little more advanced, you could do something like this:

[tcarrigan@client ~]$ printf "Hi, my name is Tyler. I write for %s. A great publication for system administrators." "Enable Sysadmin." 

Hi, my name is Tyler. I write for Enable Sysadmin. A great publication for system administrators. 

The above example is only slightly more advanced due to the usage of a STRING. The FORMAT portion of the command is contained within the double-quotes. We have a conversion spec (STRING) that outputs "Enable Sysadmin" in place of %s.

You can also use printf in conjunction with environmental variables to do some pretty neat things. A simple example of this can be tested by passing the following:

[tcarrigan@client ~]$ printf "Hello, I am %s.\n" $LOGNAME
Hello, I am tcarrigan.

This passes the STRING as the environmental variable $LOGNAME, which is the username of the account executing the command. You can see the difference below after changing accounts:

[root@client ~]# printf "I am %s.\n" g$LOGNAME
I am groot.

Sorry, I couldn't help myself.

[ Want to test your sysadmin skills? Take a skills assessment today. ] 

Wrap-up

All jokes aside, the printf command can be a handy tool, specifically if you have some experience with the C programming languages already. If not, the utility is still there, but you will have to work a bit harder to draw on the command's full power.

Topics:   Linux   Command line utilities  
Author’s photo

Tyler Carrigan

Tyler is the Sr. Community Manager at Enable Sysadmin, a submarine veteran, and an all-round tech enthusiast! He was first introduced to Red Hat in 2012 by way of a Red Hat Enterprise Linux-based combat system inside the USS Georgia Missile Control Center. More about me

Try Red Hat Enterprise Linux

Download it at no charge from the Red Hat Developer program.