English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Shell printf Command

In the previous section, we learned about the Shell echo command, and in this section, we will learn about another output command in Shell, printf.

The printf command mimics the printf() function in the C program library (library).

printf is defined by the POSIX standard, so scripts that use printf have better portability than those that use echo.

printf uses quoted text or space-separated parameters, and you can use formatted strings within printf, as well as specify the width of the string and the alignment method. By default, printf does not automatically add a newline character like echo does, and we can manually add \n.

Syntax of printf command:

printf format-string [arguments...]

Parameter description:

  • format-string: as the format control string

  • arguments: as the parameter list.

$ echo "Hello, Shell"
Hello, Shell
$ printf "Hello, Shell\n"
Hello, Shell
$

Next, I will use a script to demonstrate the powerful functions of printf:

#!/bin/bash
# author:Basic Tutorial Website
# url:www.oldtoolbag.com
 
printf "%%-10s %%-8s %%-4s\n" Name Gender Weightkg  
printf "%%-10s %%-8s %%-4.2f\n" Guo Jing Male 66.1234 
printf "%%-10s %%-8s %%-4.2f\n" Yang Guo Male 48.6543 
printf "%%-10s %%-8s %%-4.2f\n" Guo Fu Female 47.9876

Execute the script and output the result as shown below:

Name          Gender          Weightkg
Guo Jing          Male      66.12
Yang Guo          Male      48.65
Guo Fu          Female      47.99

%s %c %d %f are format placeholders,%%s output a string,%%d integer output,%%c output a character,%%f output real numbers, output in decimal form.

%-10s means a width of 10 characters (- means left alignment, without it means right alignment), any character will be displayed in 10 character wide character, if it is not enough, it will be automatically filled with spaces, and if it is exceeded, the content will be displayed in full.

%-4.2f means formatted as a decimal, where .2 to retain2decimal places.

#!/bin/bash
# author:Basic Tutorial Website
# url:www.oldtoolbag.com
 
# format-String is enclosed in double quotes
printf "%d %s\n" 1 "abc"
# Single quotes and double quotes have the same effect 
printf '%d %s\n' 1 "abc" 
# Output can also be achieved without quotes
printf %s abcdef
# Only one format is specified, but extra parameters will still be output according to the format, format-String is reused
printf %s abc def
printf "%s\n" abc def
printf "%s %s %s\n" a b c d e f g h i j
# If there are no arguments, replace %s with NULL and %d with 0
printf "%s and %d \n"

Execute the script and output the result as shown below:

1 abc
1 abc
abcdefabcdefabc
def
a b c
d e f
g h i
j  
 and 0

printf Escape Sequences

SequenceDescription
\aWarning character, usually the ASCII BEL character
\bBackspace
\cSuppress (do not display) any trailing newline characters in the output result (valid only in parameter strings controlled by the %b format specifier), and any characters left in the parameter, any subsequent parameters, and any characters left in the format string are ignored
\fFormfeed
\nNew line
\rCarriage return
\tHorizontal tab
\vVertical tab
\\a literal backslash character
\dddrepresents1to3octal value character of bit. Valid only in format strings
\0dddrepresents1to3octal value character
$ printf "a string, no processing:<%s>\n" "A\nB"
a string, no processing:<A\nB>
$ printf "a string, no processing:<%b>\n" "A\nB"
a string, no processing:<A
B>
$ printf "www.oldtoolbag.com \a"
www.oldtoolbag.com $  #No newline