Linux命令printf的用法

来源:互联网 发布:孤岛惊魂4优化 编辑:程序博客网 时间:2024/04/28 20:19

转载于:http://www.doc88.com/p-187718752176.html

参考资料:http://sns.linuxpk.com/space-566-do-blog-id-15819.html

printf FORMAT [ARGUMENT] ...

printf OPTION

【功能】

格式化并打印数据。

【举例】

打印整数和字符串:

$printf 'the integer is:%d\nthe string is:%s\n' 3 "test"

输入之后,输出如下:

the integer is:3

the string is:test

这里,使用单引号双引号都行:注意最后要有回车,否则下一个提示行和输出跑到一行了。

不用引号打印一个参数的情况:

$printf %s test

输入之后,输出如下:

test$

从这里可以看出来,test是待打印的字符,$是我的机器命令提示符号,两者在一行。

 

不用引号打印多个参数的情况:

 $printf %s first second

输入之后,输出如下:

firstsecond$

格式只指定了一个,但是多余一个参数:

$printf "%s\n" first second

输入之后,输出如下:

first

second

注意这里的回车,字符串仍旧可以输出。

指定的格式少于参数:

$printf "%s %s %s\n" a b c d e f g h i j k l m n

输入之后,输出如下:

a b c d

e f g h

i j k l

m n

 

原创粉丝点击