Linux Command echo
tags: 文本處理
文章目錄
- Linux Command echo
- 1. 簡介
- 2. 轉義
- 3. 用法
- 标準輸出
- 顯示變量
- 顯示轉義字元
- 顯示檔案
- 輸出比對檔案
- 定向輸入檔案
- 彩色列印
- 顯示指令執行結果
- \b 選項删除字元間的所有空格
- \n 選項換行
- \t 選項加水準制表符
- 換行 \n 與水準制表符 \t
- \v 選項垂直制表符
- 換行 \n 與垂直制表符 \v
- \r 選項回車符
- \c 選項不換行
- -n 會在echo完後不會輸出新行
- \a 選項聲音警告
- 腳本
- feed.sh
1. 簡介
echo是一種最常用的與廣泛使用的内置于Linux的bash和C shell的指令,通常用在腳本語言和批處理檔案中來在标準輸出或者檔案中顯示一行文本或者字元串。
echo是一個内置shell指令。echo的行為與其他流行的shell一樣,如Zsh和Ksh。但它們的行為與shell之間略有不同。
2. 轉義
選項 | 描述 |
-n | 不輸出末尾的換行符。 |
-e | 啟用反斜線轉義。 |
\b | 倒退 |
\ | 反斜線 |
\n | 新行 |
\r | 回車 |
\t | 水準制表符 |
\v | 垂直制表符 |
3. 用法
标準輸出
$ echo
顯示變量
比如,聲明變量x并給它指派為10。
$ x=10
$ echo The value of variable x = $x
The value of variable x = 10
顯示轉義字元
$ echo "\"It is a test\""
"It is a test"
顯示檔案
$ echo *
103.odt 103.pdf 104.odt 104.pdf 105.odt 105.pdf 106.odt 106.pdf 107.odt 107.pdf 108a.odt 108.odt 108.pdf 109.odt 109.pdf 110b.odt 110.odt 110.pdf 111.odt 111.pdf 112.odt 112.pdf 113.odt linux-headers-3.16.0-customkernel_1_amd64.deb linux-image-3.16.0-customkernel_1_amd64.deb network.jpeg
輸出比對檔案
比如,讓我們假設你想要列印所有的
‘.jpeg‘
檔案,使用下面的指令。
$ echo
定向輸入檔案
$ echo "Test Page" > testpage
$ cat
彩色列印
你可使用ANSI轉義序列更改前景色和背景色或設定下劃線和粗體等文本屬性。
echo -e "\033[1;37mWHITE" #列印白色
echo -e "\033[0;30mBLACK" #列印黑色
echo -e "\033[0;34mBLUE" #列印藍色
echo -e "\033[0;32mGREEN" #列印綠色
echo -e "\033[0;36mCYAN" #列印青藍色
echo -e "\033[0;31mRED" #列印紅色
echo -e "\033[0;35mPURPLE" #列印紫色。
顯示指令執行結果
$ echo `date`
Tue Jun 28 11:59:39 UTC 2022
\b 選項删除字元間的所有空格
- -e‘後帶上’\b’會删除字元間的所有空格。
[!NOTE|style:flat|lable:Mylable|iconVisibility:hidden]
Linux中的選項‘-e‘扮演了轉義字元反斜線的翻譯器。
$ echo -e "Tecmint \bis \ba \bcommunity \bof \bLinux \bNerds"
\n 選項換行
- -e‘後面的帶上‘\n’行會換行
$ echo -e "Tecmint \nis \na \ncommunity \nof \nLinux \nNerds"
\t 選項加水準制表符
- -e‘後面跟上‘\t’會在空格間加上水準制表符。
$ echo -e "Tecmint \tis \ta \tcommunity \tof \tLinux \tNerds"
換行 \n 與水準制表符 \t
$ echo -e "\n\tTecmint \n\tis \n\ta \n\tcommunity \n\tof \n\tLinux \n\tNerds"
\v 選項垂直制表符
- ‘-e‘後面跟上‘\v’會加上垂直制表符。
$ echo -e "\vTecmint \vis \va \vcommunity \vof \vLinux \vNerds"
換行 \n 與垂直制表符 \v
$ echo -e "\n\vTecmint \n\vis \n\va \n\vcommunity \n\vof \n\vLinux \n\vNerds"
[!NOTE|style:flat|lable:Mylable|iconVisibility:hidden]
你可以按照你的需求連續使用兩個或者多個垂直制表符,水準制表符與換行符。
\r 選項回車符
- -e‘後面跟上‘\r’來指定輸出中的回車符。(LCTT 譯注:會覆寫行開頭的字元)
$ echo -e "Tecmint \ris a community of Linux Nerds"
\c 選項不換行
- -e‘後面跟上‘\c’會抑制輸出後面的字元并且最後不會換新行。
$ echo -e "Tecmint is a community \cof Linux Nerds"
-n 會在echo完後不會輸出新行
$ echo -n "Tecmint is a community of Linux Nerds"
\a 選項聲音警告
- -e‘後面跟上‘\a’選項會聽到聲音警告。
$ echo -e "Tecmint is a community of \aLinux Nerds"
腳本
feed.sh
$ cat feed.sh
#!/bin/bash
# This script acts upon the exit status given by penguin.sh
if [ "$#" != "2" ]; then
echo -e "Usage of the feed script:\t$0 food-on-menu animal-name\n"
exit 1
else
export menu="$1"
export animal="$2"
echo -e "Feeding $menu to $animal...\n"
feed="/nethome/anny/testdir/penguin.sh"
$feed $menu $animal
result="$?"
echo -e "Done feeding.\n"
case "$result" in
1)
echo -e "Guard: \"You'd better give'm a fish, less they get violent...\"\n"
;;
2)
echo -e "Guard: \"No wonder they flee our planet...\"\n"
;;
3)
echo -e "Guard: \"Buy the food that the Zoo provides at the entry, you ***\"\n"
echo -e "Guard: \"You want to poison them, do you?\"\n"
;;
*)
echo -e "Guard: \"Don't forget the guide!\"\n"
;;
esac
fi
echo "Leaving..."
echo -e "\a\a\aThanks for visiting the Zoo, hope to see you again soon!\n"
michel ~/test> feed.sh apple camel
Feeding apple to camel...
Will you read this sign?! Don't feed the camels!
Done feeding.
Guard: "Buy the food that the Zoo provides at the entry, you ***"
Guard: "You want to poison them, do you?"
Leaving...
Thanks for visiting the Zoo, hope to see you again soon!
michel ~/test>
- runoob Shell echo指令
- Using the echo built-in command
- 15 Practical Examples of ‘echo’ command in Linux