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 * * * * * * * * * * * * * * * *