Easy Tutorial
❮ Linux Comm Fdisk Linux Comm Rdate ❯

Shell printf Command

In the previous chapter, we learned about the Shell's echo command. In this chapter, we will learn about another output command, printf.

The printf command mimics the printf() function from the C programming library.

printf is defined by the POSIX standard, making scripts that use printf more portable than those using echo.

printf uses quoted text or space-separated arguments. You can use formatted strings in printf, and specify the width of the string, alignment, etc. Unlike echo, printf does not automatically add a newline character by default; you can manually add \n.

The syntax of the printf command is:

printf format-string [arguments...]

Parameter Description:

Example

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

Next, I will demonstrate the powerful functionality of printf with a script:

Example

#!/bin/bash
# author:tutorialpro.org
# url:www.tutorialpro.org

printf "%-10s %-8s %-4s\n" 姓名 性别 体重kg  
printf "%-10s %-8s %-4.2f\n" 郭靖 男 66.1234 
printf "%-10s %-8s %-4.2f\n" 杨过 男 48.6543 
printf "%-10s %-8s %-4.2f\n" 郭芙 女 47.9876

Executing the script, the output is as follows:

姓名     性别   体重kg
郭靖     男      66.12
杨过     男      48.65
郭芙     女      47.99

%s %c %d %f are format substitution characters. %s outputs a string, %d outputs an integer, %c outputs a character, and %f outputs a real number in decimal format.

%-10s specifies a width of 10 characters (- indicates left alignment; without it, it indicates right alignment). Any characters will be displayed within a 10-character wide field. If insufficient, spaces will automatically fill the field; if exceeded, the content will be fully displayed.

%-4.2f specifies formatting as a decimal, where .2 indicates retaining two decimal places.

Example

#!/bin/bash
# author:tutorialpro.org
# url:www.tutorialpro.org

# format-string is 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 without quotes is also possible
printf %s abcdef

# The format specifies only one parameter, but additional parameters will still be output according to this format, with format-string being 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, %s is replaced with NULL and %d with 0
printf "%s and %d \n"

Executing the script, the output is as follows:

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

printf Escape Sequences

Sequence Description
\a Alert character, usually the ASCII BEL character
\b Backspace
\c Suppress any trailing newline characters (valid only within %b format control strings), and ignore any remaining characters in the argument, any subsequent arguments, and any remaining characters in the format string
\f Form feed
\n Newline
\r Carriage return
\t Horizontal tab
\v Vertical tab
\ A literal backslash character
\ddd A character with a value of an 1 to 3-digit octal number. Valid only within format strings
\0ddd A character with a value of an 1 to 3-digit octal number

Example

$ 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:&lt;A
B>

$ printf "www.tutorialpro.org \a" www.tutorialpro.org $ # No line break

❮ Linux Comm Fdisk Linux Comm Rdate ❯