shell 脚本之 printf

来源:互联网 发布:relief算法python实现 编辑:程序博客网 时间:2024/04/28 19:30

shell 脚本之printf

  • 上篇转载了echo的用法,那么我们本篇就讲一下shell 脚本的格式化输出。shell 的printf基本上继承了 c语言的printf 函数的格式,你只要会c语言的printf 函数,那么shell 脚本的printf 函数对于你来说也没有什么难度。
  • 参考了这篇博客:http://blog.csdn.net/shanyongxu/article/details/46744055

    先上代码:

#! /bin/bashprintf "%-50s %-20s \n" first second #第一个字符串50个字符左对齐,第二个20个字符左对齐,\n 代表换行printf "%50s %20s \n" first second #默认右对齐printf "%4d %4d \n" 1234 12345  #输出整型printf "%s %s %s \n" a b c d e f g h i j #多个参数,每行输出三个字符printf "%s %s hello \n" first second  #hello为在参数后输出的字符串printf "%04d %10.2f \n" 123 9 #不足部位,4 位整数不足补0,保留两位小数

最后看一下结果:
这里写图片描述


是不是很简单,那就自己动手做一下吧。

0 0