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.
printf command traces its roots back to the development world but offers practical utility to the sysadmin, as well. Derived from the
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:
|\c||produce no further output|
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)|
|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 ]
The most basic use cases for
printf is outputting strings of text without receiving an error.
[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
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. ]
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.