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.
À propos de l'auteur
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. Now that he has surfaced, he lives with his wife and son near Raleigh, where he worked as a data storage engineer before finding his way to the Red Hat team. He has written numerous technical documents, from military procedures to knowledgebase articles and even some training curricula. In his free time, he blends a passion for hiking, climbing, and bushcraft with video games and computer building. He is loves to read and enjoy a scotch or bourbon. Find him on Twitter or on LinkedIn.
Plus de résultats similaires
More than meets the eye: Behind the scenes of Red Hat Enterprise Linux 10 (Part 4)
Why should your organization standardize on Red Hat Enterprise Linux today?
The Overlooked Operating System | Compiler: Stack/Unstuck
Linux, Shadowman, And Open Source Spirit | Compiler
Parcourir par canal
Automatisation
Les dernières nouveautés en matière d'automatisation informatique pour les technologies, les équipes et les environnements
Intelligence artificielle
Actualité sur les plateformes qui permettent aux clients d'exécuter des charges de travail d'IA sur tout type d'environnement
Cloud hybride ouvert
Découvrez comment créer un avenir flexible grâce au cloud hybride
Sécurité
Les dernières actualités sur la façon dont nous réduisons les risques dans tous les environnements et technologies
Edge computing
Actualité sur les plateformes qui simplifient les opérations en périphérie
Infrastructure
Les dernières nouveautés sur la plateforme Linux d'entreprise leader au monde
Applications
À l’intérieur de nos solutions aux défis d’application les plus difficiles
Virtualisation
L'avenir de la virtualisation d'entreprise pour vos charges de travail sur site ou sur le cloud