linux中打印有echo和printf linux
1、echo shell
echo是用于终端打印的基本命令,默认状况下,echo会在每次调用后添加一个换行符。若是要忽略换行,能够echo后面,字符窜以前加上"-n". bash
echo后面打印的内容,能够是双引号,单引号,或者什么也不加(若是带有特殊字符时,例如"!",须要在"!"前面加上转义字符"\",或者不要把"!"放在双引号里) spa
$ echo hello world !
或者 code
$ echo 'hello world !'
或者 class
$ echo "hello world \!"
每种方法反作用: 变量
echo接受双引号字符窜内的转义序列做为参数,格式为 echo -e "包含转义序列的字符窜" 终端
例如: 引用
$ echo -e "1\t2\t3" 1 2 3
打印彩色输出,能够使用转义序列实现 方法
$ echo -e "\e[1;31m This is color text \e[0m"
This is color text
\e[1;31 将颜色设为红色,\e[0m 将颜色置回,你只须要把31变成你想要的颜色码就好了
(0=重置 ,30=黑色,31=红色,32=绿色,33=黄色,34=蓝色,35=洋红,36=青色,37=白色)
2、printf
printf使用引用文本或者由空格分隔的参数。咱们能够在printf中使用格式化字符窜,还能够指定字符窜的宽度,左右对其方式。
在默认状况下,printf须要手工添加换行符"\n"
$ printf "Hello world \n"
打印星星,建立hello.sh
#!/bin/bash #文件名:hello.sh printf "%10s\n" "*" printf "%8s %3s\n" "*" "*" printf "%6s %7s\n" "*" "*" printf "%4s %11s\n" "*" "*" printf "%2s %15s\n" "*" "*" printf "%4s %11s\n" "*" "*" printf "%6s %7s\n" "*" "*" printf "%8s %3s\n" "*" "*" printf "%10s\n"
运行
$ sh hello.sh
效果以下
$ sh hello.sh * * * * * * * * * * * * * * * *