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.


저자 소개

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.

UI_Icon-Red_Hat-Close-A-Black-RGB

채널별 검색

automation icon

오토메이션

기술, 팀, 인프라를 위한 IT 자동화 최신 동향

AI icon

인공지능

고객이 어디서나 AI 워크로드를 실행할 수 있도록 지원하는 플랫폼 업데이트

open hybrid cloud icon

오픈 하이브리드 클라우드

하이브리드 클라우드로 더욱 유연한 미래를 구축하는 방법을 알아보세요

security icon

보안

환경과 기술 전반에 걸쳐 리스크를 감소하는 방법에 대한 최신 정보

edge icon

엣지 컴퓨팅

엣지에서의 운영을 단순화하는 플랫폼 업데이트

Infrastructure icon

인프라

세계적으로 인정받은 기업용 Linux 플랫폼에 대한 최신 정보

application development icon

애플리케이션

복잡한 애플리케이션에 대한 솔루션 더 보기

Virtualization icon

가상화

온프레미스와 클라우드 환경에서 워크로드를 유연하게 운영하기 위한 엔터프라이즈 가상화의 미래