printf命令

printf命令


printf命令需要在字符串中使用占位符,然后指定要输出的值。它可以通过在字符串中包含一些格式说明符,然后按照格式说明符指定的格式将参数输出到标准输出。例如:

[root@xxx ~]# printf "%-10s %-8s %-4s\n" 姓名 性别 体重/kg
[root@xxx ~]# printf "%-10s %-8s %-4.2f\n" 郭靖 男 66.1234
[root@xxx ~]# printf "%-10s %-8s %-4.2f\n" 杨过 男 68.6543
[root@xxx ~]# printf "%-10s %-8s %-4.2f\n" 郭芙 女 47.9876
姓名     性别   体重/kg
郭靖     男      66.12
杨过     男      68.65
郭芙     女      47.99

•示例中,%-10s表示输出一个左对齐且宽度为10的字符串,%-8s表示输出一个左对齐且宽度为8的字符串,%-4.2f表示输出一个左对齐且宽度为4,小数点后保留2位的浮点数。

一、printf命令使用%c格式化字符的示例如下:

(1)输出单个字符:

[root@xxx ~]#  printf "The first letter of the alphabet is %c\n" 'a'
The first letter of the alphabet is a

(2)输出字符数组中的所有字符:

[root@xxx ~]# characters=('a' 'b' 'c')
[root@xxx ~]# printf "The characters are:%c %c %c\n" "${characters[@]}"
The characters are:a b c

(3)输出字符变量的值:

[root@xxx ~]# letter='Z'
[root@xxx ~]# printf "The letter is:%c\n" "$letter"
The letter is:Z

二、printf命令使用%s格式化字符串的示例如下:

(1)输出单个字符串:

[root@xxx ~]# printf "The name of this website is %s\n" "Stack Overflow"
The name of this website is Stack Overflow

(2)输出字符串数组中的所有字符串:

names=('Alice''Bob''Eve')
[root@xxx ~]# printf "The names are:%s %s %s\n""${names[@]}"
The names are:Alice Bob Eve

(3)输出字符变量的值:

[root@xxx ~]# greeting='Hello,world!'
[root@xxx ~]# printf "The greeting is:%s\n" "$greeting"
The greeting is:Hello,world!

三、printf命令使用%d格式化字符串的示例如下:

(1)输出单个整数:

[root@xxx ~]# printf "The number is:%d\n" 42
The number is:42

(2)输出字符串数组中的所有字符串:

[root@xxx ~]# numbers=(1 2 3)
[root@xxx ~]# printf "The numbers are:%d %d %d\n" "${numbers[@]}"
The numbers are:1 2 3

(3)输出字符变量的值:

[root@xxx ~]# count=5
[root@xxx ~]# printf "The count is:%d\n" "$count"
The count is:5

四、printf命令使用%b格式化字符串的示例如下:

(1)输出单个二进制整数:

[root@xxx ~]# printf "The number is: %b\n" 5 
The number is: 5

(2)输出字符串数组中的所有字符串:

[root@xxx ~]# numbers=(5 6 7)
[root@xxx ~]# printf "The numbers are:%b %b %b\n" "${numbers[@]}"
The numbers are:5 6 7

(3)输出字符变量的值:

[root@xxx ~]# binary=1101
[root@xxx ~]# printf "The binary number is:%b\n" "$binary"
The binary number is:1101

五、printf命令使用%n格式化字符串的示例如下:

(1)输出单个二进制整数:

[root@xxx ~]# printf "There are %d characters in this string.%n" 8 count
There are 8 characters in this string.
[root@xxx ~]# echo "The value of count is:$count"
The value of count is:38

(2)在字符串数组中使用%n:

[root@xxx ~]# strings=('This is string1''This is string 2')
[root@xxx ~]# printf "There are %d characters in string 1.%n" 100 count1
There are 100 characters in string 1.
[root@xxx ~]# printf "There are %d characters in string 2.%n" 1000 count2
There are 1000 characters in string 2.
[root@xxx ~]# echo "The value of count1 is:$count1"
The value of count1 is:37
[root@xxx ~]# echo "The value of count2 is:$count2"
The value of count2 is:38

六、printf命令使用%(datefmt)T格式化日期和时间的示例如下:

(1)输出当前日期和时间:

[root@xxx ~]# printf "The current date and time is: %(%Y-%m-%d %H:%M:%S)T\n"
The current date and time is: 2024-09-20 15:20:58

(2)在字符串数组中使用%n:

[root@xxx ~]# printf "The date and time is: %(%a %b %d %I:%M %p %Z %Y)T\n"
The date and time is: 五 920 03:21 下午 CST 2024

2)在字符串数组中使用%n:

[root@xxx ~]# printf "The date and time is: %(%a %b %d %I:%M %p %Z %Y)T\n"
The date and time is: 五 920 03:21 下午 CST 2024
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值