Linux 之 printf 输出命令

在 菜鸟教程 上学习的,请大家多多指教。

正式开始:

printf 命令的语法:

printf  format-string  [arguments...]

参数说明:

  • format-string: 为格式控制字符串。(备注:printf 不会像 echo 一样添加换行符,只能手动添加)
  • arguments: 为参数列表

1、简单举例。格式控制字符串用 单引号 或者 双引号 效果是一样的。

[root@localhost home]# printf "%d %s\n" 1 'hello'
1 hello
[root@localhost home]# printf '%d %s\n' 1 'hello'
1 hello

2、取变量

[root@localhost home]# a=10
[root@localhost home]# b='hello'
[root@localhost home]# printf '%d %s\n' $a $b
10 hello 

3、若参数为 字符串 时,可以不用加 引号。

[root@localhost home]# printf '%s %s\n' a b
a b

或者如下,但是必须要用 双引号,因为 单引号字符串 中的变量是无效的:

[root@localhost home]# printf "$a $b\n"
10 hello

4、跟 C中几乎一样,格式控制字符串中,可以加:字符串的输出长度,左/右对齐,小数点的位数.....等等。

      注:%-10s : 表示 10 个字符宽度的字符串且左对齐( - :左对齐,没有 - :右对齐)。一个中文占 3 个字符。

             %-5.2f  表示 小数点后保留 2 位且左对齐。5 表示 总的字符数,小数点也算一位。不足 5 位时,在右边补 空格;

             %5.2f  表示 小数点后保留 2 位且右对齐,不足 5 位时,在左边补 空格。

[root@localhost home]# printf "%-10s %-8s %-4s\n" 姓名 性别 体重kg
姓名     性别   体重kg

[root@localhost home]# printf "%-10s %-8s %-5.2f\n" 郭靖 男 1266.1234
郭靖     男      1266.12

[root@localhost home]# printf "%-10s %-8s %-5.2fabc\n" 郭靖 男 1.12
郭靖     男      1.12 abc                #1.12+:小数点后保留 2 位且左对齐,加整数 1 和 小数点 . 共 4 位,右边补空格,+表示空格。

[root@localhost home]# printf "%-10s %-8s %5.2fabc\n" 郭靖 男 1.12
郭靖     男        1.12abc               #+1.12:小数点后保留 2 位且右对齐,加整数 1 和 小数点 . 共 4 位,左边补空格,+表示空格。

5、格式只指定了一个参数,但多出的参数仍然会按照该格式输出,format-string 被重用。

[root@localhost home]# printf %s abcdef                  # 格式控制字符串没有引号,也可以输出,但是不会换行
abcdef[root@localhost home]

[root@localhost home]# printf %s"\n" abcdef            # 在上面的基础上加 换行 "\n"。或者 "%s\n"、'%s\n'。
abcdef

[root@localhost home]# printf %s hello world           # 只有一个格式控制符,但是有两个参数,会输出,但是中间没有 空格。
helloworld[root@localhost home]#

[root@localhost home]# printf %s"\n" abc def           # 在上面的基础上加 换行 "\n"。或者 "%s\n"、'%s\n'。
abc
def

[root@localhost home]# printf "%s %s %s\n" a b c d e f g h i j
a b c
d e f
g h i

6、如果没有 arguments,那么 %s 用NULL代替,%d 用 0 代替

[root@localhost home]# printf "%s and %d \n"
 and 0               #+and 0:+ 为空格。

7、转义序列。

[root@localhost home]# printf "\a"                          # 警告字符,通常为ASCII的 BEL 字符。会发出 响声。

[root@localhost home]# printf "abc\b"                    # 退格。输出 abc 后,往后退一格。ab
ab[root@localhost home]#

[root@localhost home]# printf "abc\b\n"                 # 直接换行。相当于 "abc\n"。
abc

[root@localhost home]# printf "abc\n\b"                 # 同上,直接换行。相当于 "abc\n"。
abc

[root@localhost home]# printf "abc\fhello"             # 换页符。hello 前面缩进与 abc 长度有关,在word 中,abc 和 hello 各一页。
abc
      hello[root@localhost home]#

[root@localhost home]# printf "abc\nhello"                  # 换行。
abc
hello[root@localhost home]#

[root@localhost home]# printf "abc\rhello"                   # 回车。\r 前面的字符没有了。
hello[root@localhost home]#

[root@localhost home]# printf "abc\nworld\rhello\n"                # 输出 abc 后换行,world “消失”,再输出 hello 并换行。
abc
hello

[root@localhost home]# printf "abc\thello\n"                 # 水平制表符,占 8 位(我这里环境),有的可能是 4 位。
abc    hello                                                                          # hello 前面是 5 个空格

[root@localhost home]# printf "abcdefg\thello\n"
abcdefg hello                                                                      # abcdefg+:7个字符加一个 空格

[root@localhost home]# printf "abcdefgh\thello\n"
abcdefgh           hello                                                          # abcdefgh++++++++:一共 8 个字符再加 8 个空格

[root@localhost home]# printf "abcdefgh\vhello\n"                 # 跟上面的 “\f” 换页符有点类似,但是 abcdefgh 和 hello 在同一页,而 '\f' 前后的内容是在不同页。 
abcdefgh
               hello

[root@localhost home]# printf "\101"                          # \ddd:表示1到3位数八进制值的字符。仅在格式字符串中有效。64 + 1
A

[root@localhost home]# printf "\141"                          # 1*8*8 + 4*8 + 1 = 97
a

上面很多是参考 菜鸟教程,增加了例子。

猜你喜欢

转载自blog.csdn.net/tk1023/article/details/108750009
今日推荐