Shell printf命令的用法(非常详细)
在 Shell 脚本中,printf 命令主要用于格式化输出字符串,输出带有特定格式的信息,如输出字符串、数字、字符、符号或者其他值。它与 echo 命令类似,但支持更多的格式化选项。
printf 命令的基本语法如下:
printf 命令需要在字符串中使用占位符,然后指定要输出的值。它可以通过在字符串中包含一些格式说明符,并按照格式说明符指定的格式将参数输出到标准输出。例如:
printf 命令使用的占位符如下表所示:
printf 命令使用
1) 输出单个字符,例如:
2) 输出字符数组中的所有字符,例如:
3) 输出字符变量的值,例如:
printf 命令使用
1) 输出单个字符串,例如:
2) 输出字符串数组中的所有字符串,例如:
3) 输出字符串变量的值,例如:
printf 命令使用
1) 输出单个整数,例如:
2) 输出整数数组中的所有数字,例如:
3) 输出整数变量的值,例如:
printf 命令使用
1) 输出单个二进制整数,例如:
2)输出二进制整数数组中的所有数字,例如:
3) 输出二进制整数变量的值,例如:
printf 命令使用
1) 在字符串中使用 %n,例如:
2) 在字符串数组中使用
printf 命令使用
1) 输出当前日期和时间,例如:
2) 输出使用自定义格式的日期和时间,例如:
printf 命令的基本语法如下:
printf format [argument...]其中,format 表示一个字符串,用于指定输出的格式,它可以包含转义序列,这些转义序列用于指定输出的格式和内容;argument 表示一个或多个参数,用于提供要输出的内容。
printf 命令需要在字符串中使用占位符,然后指定要输出的值。它可以通过在字符串中包含一些格式说明符,并按照格式说明符指定的格式将参数输出到标准输出。例如:
printf "%-10s %-8s %-4s\n" 姓名 性别 体重/kg printf "%-10s %-8s %-4.2f\n" 郭靖 男66.1234 printf "%-10s %-8s %-4.2f\n" 杨过 男68.6543 printf "%-10s %-8s %-4.2f\n" 郭芙 女47.9876输出结果如下:
姓名 性别 体重/kg 郭靖 男 66.12 杨过 男 68.65 郭芙 女 47.99在这个示例中,
%-10s
表示输出一个左对齐且宽度为 10 的字符串;%-8s
表示输出一个左对齐且宽度为 8 的字符串;%-4.2f
表示输出一个左对齐且宽度为 4,小数点后保留 2 位的浮点数。printf 命令使用的占位符如下表所示:
序号 | 占位符 | 说明 |
---|---|---|
1 | %s | 输出字符串(string) |
2 | %c | 输出单个字符(character) |
3 | %d | 输出十进制整数(decimal integer) |
4 | %f | 输出浮点数(floating point number) |
5 | %o | 输出无符号八进制整数(octal integer) |
6 | %e | 输出科学记数法(scientific notation)形式的浮点数 |
7 | %b | 输出二进制整数(binary integer) |
8 | %n | 输出目前为止输出的字符总数 |
9 | %g | 输出指定精度的浮点数 |
10 | %x | 输出无符号十六进制整数(hexadecimal integer)(小写字母形式) |
11 | %X | 输出十六进制整数(大写字母形式) |
12 | %(datefmt)T | 将参数以指定的日期和时间格式输出(datefmt为日期和时间格式字符串) |
printf 命令使用
%c
格式化字符的示例如下。1) 输出单个字符,例如:
printf "The first letter of the alphabet is %c\n" 'a'输出结果如下:
The first letter of the alphabet is a
2) 输出字符数组中的所有字符,例如:
characters=('a' 'b' 'c') printf "The characters are: %c %c %c\n" "${characters[@]}"输出结果如下:
The characters are: a b c
3) 输出字符变量的值,例如:
letter='Z' printf "The letter is: %c\n" "$letter"输出结果如下:
The letter is: Z
printf 命令使用
%s
格式化字符串的示例如下。1) 输出单个字符串,例如:
printf "The name of this website is %s\n" "Stack Overflow"输出结果如下:
The name of this website is Stack Overflow
2) 输出字符串数组中的所有字符串,例如:
names=('Alice' 'Bob' 'Eve') printf "The names are: %s %s %s\n" "${names[@]}"输出结果如下:
The names are: Alice Bob Eve
3) 输出字符串变量的值,例如:
greeting='Hello, world!' printf "The greeting is: %s\n" "$greeting"输出结果如下:
The greeting is: Hello, world!
printf 命令使用
%d
格式化整数的示例如下。1) 输出单个整数,例如:
printf "The number is: %d\n" 42输出结果如下:
The number is: 42
2) 输出整数数组中的所有数字,例如:
numbers=(1 2 3) printf "The numbers are: %d %d %d\n" "${numbers[@]}"输出结果如下:
The numbers are: 1 2 3
3) 输出整数变量的值,例如:
count=5 printf "The count is: %d\n" "$count"输出结果如下:
The count is: 5
printf 命令使用
%b
格式化二进制整数的示例如下。1) 输出单个二进制整数,例如:
printf "The number is: %b\n" 5输出结果如下:
The number is: 101
2)输出二进制整数数组中的所有数字,例如:
numbers=(5 6 7) printf "The numbers are: %b %b %b\n" "${numbers[@]}"输出结果如下:
The numbers are: 5 6 7
3) 输出二进制整数变量的值,例如:
binary=1101 printf "The binary number is: %b\n" "$binary"输出结果如下:
The binary number is: 1101
printf 命令使用
%n
输出字符总数的示例如下。1) 在字符串中使用 %n,例如:
printf "There are %d characters in this string.%n" 8 count echo "The value of count is: $count"输出结果如下:
There are 14 characters in this string.
The value of count is: 38
2) 在字符串数组中使用
%n
,例如:
strings=('This is string 1' 'This is string 2') printf "There are %d characters in string 1.%n" 100 count1 printf "There are %d characters in string 2.%n" 1000 count2 echo "The value of count1 is: $count1" echo "The value of count2 is: $count2"输出结果如下:
There are 100 characters in string 1. There are 1000 characters in string 2. The value of count1 is: 37 The value of count2 is: 38
printf 命令使用
%(datefmt)T
格式化日期和时间的示例如下。1) 输出当前日期和时间,例如:
printf "The current date and time is: %(%Y-%m-%d %H:%M:%S)T\n"输出结果如下:
The current date and time is: 2025-01-08 20:41:08
2) 输出使用自定义格式的日期和时间,例如:
printf "The date and time is: %(%a %b %d %I:%M %p %Z %Y)T\n"输出结果如下:
The date and time is: Sun Jan 08 08:44 PM CST 2025