Linux上printf命令的用法 printf格式化输出 基本格式 printf [format] [文本1] [文本2] .. 常用格式替换符 %s 字符串 %f 浮点格式 %c ASCII字符,即显示对应参数的第一个字符 %d,%i 十进制整数 %o 八进制值 %u 不带正负号的十进制值 %x 十六进制值(a-f) %X 十六进制值(A-F) %% 表示%本身 常用转义字符 \a 警告字符,通常为ASCII的BEL字符 \b 后退 \f 换页 \n 换行 \r 回车 \t 水平制表符 \v 垂直制表符 \\ 表示\本身 -------------------- 使用示例 [keysystem@localhost ~]$ printf "%s\n" 1 2 3 4 1 2 3 4 [keysystem@localhost ~]$ printf "%f\n" 1 2 3 4 1.000000 2.000000 3.000000 4.000000 [keysystem@localhost ~]$ printf "%.2f\n" 1 2 3 4 1.00 2.00 3.00 4.00 [keysystem@localhost ~]$ printf " (%s) " 1 2 3 4;echo "" (1) (2) (3) (4) [keysystem@localhost ~]$ printf "%s %s\n" 1 2 3 4 1 2 3 4 [keysystem@localhost ~]$ printf "%s %s %s\n" 1 2 3 4 1 2 3 4 [keysystem@localhost ~]$ [keysystem@localhost ~]$ #"-"表示左对齐, "10 10 4 4" 表示占的字符位数, 不够不空格 [keysystem@localhost ~]$ printf "%-10s %-10s %-4s %-4s \n" 姓名 性别 年龄 体重 苹果 男 18 60 香蕉 男 18 80 姓名 性别 年龄 体重 苹果 男 18 60 香蕉 男 18 80 [keysystem@localhost ~]$ printf "%X" 13 #10进制转16进制 D[keysystem@localhost ~]$ printf "%X\n" 13 D [keysystem@localhost ~]$ printf "%d" 0xB #16进制转10进制 11 其它连接: https://www.cnblogs.com/kingle-study/p/9336721.html linux printf 不立即显示 1.加上\n换行符,使得缓冲区立即输出 2. fflush(stdout); //  刷新一下缓冲区 让它马上输出.  在printf 之后调用它,就会马上输出了 !!setvbuf(stdout,NULL,_IONBF,0); //如果你嫌上个方法麻烦, 就使用这个函数. 直接将缓冲区禁止了. 它就直接输出了.这两个函数都是有关流缓冲区的. 具体使用和说明网上有很多.我只说一下什么是流缓冲区,是做什么用的. 操作系统为减少 IO操作 所以设置了缓冲区.等缓冲区满了再去操作IO.这样是为了提高效率. 作者:tianming1992 链接:https://www.jianshu.com/p/d77b8e1f20e6 来源:简书