Linux下echo命令的用法
echo是一種最常用的與廣泛使用的內建於Linux的bash和C shell的命令,通常用在指令碼語言和批處理檔案中來在標準輸出或者檔案中顯示一行文字或者字串。
echo命令的語法是:
echo [選項][字串]
1. 輸入一行文字並顯示在標準輸出上
$ echo Tecmintis acommunity of LinuxNerds
會輸出下面的文字:
Tecmintis acommunity of LinuxNerds
2. 輸出一個宣告的變數值
比如,宣告變數x並給它賦值為10。
$ x=10
會輸出它的值:
$ echo The value ofvariable x = $x
The value ofvariable x =10
3. 使用‘\b‘選項
‘-e‘後帶上'\b'會刪除字元間的所有空格。
注意: Linux中的選項‘-e‘扮演了轉義字元反斜線的翻譯器。
$ echo -e"Tecmint \bis \ba \bcommunity \bof \bLinux \bNerds"
TecmintisacommunityofLinuxNerds
4. 使用‘\n‘選項
‘-e‘後面的帶上‘\n’行會在遇到的地方作為新的一行
$ echo -e"Tecmint \nis \na \ncommunity \nof \nLinux \nNerds"
Tecmint
is
a
community
of
Linux
Nerds
5. 使用‘\t‘選項
‘-e‘後面跟上‘\t’會在空格間加上水平製表符。
$ echo -e"Tecmint \tis \ta \tcommunity \tof \tLinux \tNerds"
Tecmintis acommunity of LinuxNerds
6. 也可以同時使用換行‘\n‘與水平製表符‘\t‘
$ echo -e"\n\tTecmint \n\tis \n\ta \n\tcommunity \n\tof \n\tLinux \n\tNerds"
Tecmint
is
a
community
of
Linux
Nerds
7. 使用‘\v‘選項
‘-e‘後面跟上‘\v’會加上垂直製表符。
$ echo -e"\vTecmint \vis \va \vcommunity \vof \vLinux \vNerds"
Tecmint
is
a
community
of
Linux
Nerds
8. 也可以同時使用換行‘\n‘與垂直製表符‘\v‘
$ echo -e"\n\vTecmint \n\vis \n\va \n\vcommunity \n\vof \n\vLinux \n\vNerds"
Tecmint
is
a
community
of
Linux
Nerds
注意: 你可以按照你的需求連續使用兩個或者多個垂直製表符,水平製表符與換行符。
9. 使用‘\r‘選項
‘-e‘後面跟上‘\r’來指定輸出中的回車符。(LCTT 譯註:會覆寫行開頭的字元)
$ echo -e"Tecmint \ris a community of Linux Nerds"
is a community ofLinuxNerds
10. 使用‘\c‘選項
‘-e‘後面跟上‘\c’會抑制輸出後面的字元並且最後不會換新行。
$ echo -e"Tecmint is a community \cof Linux Nerds"
Tecmintis acommunity @tecmint:~$
11. ‘-n‘會在echo完後不會輸出新行
$ echo -n"Tecmint is a community of Linux Nerds"
Tecmintis acommunity of LinuxNerds@tecmint:~/Documents$
12. 使用‘\a‘選項
‘-e‘後面跟上‘\a’選項會聽到聲音警告。
$ echo -e"Tecmint is a community of \aLinux Nerds"
Tecmintis acommunity of LinuxNerds
注意: 在你開始前,請先檢查你的音量設定。
13. 使用echo命令列印所有的檔案和資料夾(ls命令的替代)
$ echo *
103.odt103.pdf104.odt104.pdf105.odt105.pdf106.odt106.pdf107.odt107.pdf108a.odt108.odt108.pdf109.odt109.pdf110b.odt 110.odt110.pdf111.odt111.pdf112.odt112.pdf113.odtlinux-headers-3.16.0-customkernel_1_amd64.deblinux-image-3.16.0-customkernel_1_amd64.deb network.jpeg
14. 列印製定的檔案型別
比如,讓我們假設你想要列印所有的‘.jpeg‘檔案,使用下面的命令。
$ echo *.jpeg
network.jpeg
15. echo可以使用重定向符來輸出到一個檔案而不是標準輸出
$ echo "TestPage"> testpage
## Check Content
avi@tecmint:~$ cattestpage
TestPage
相關文章
- Linux基礎命令:echo的使用Linux
- Linux——基礎命令用法(下)Linux
- linux命令下jq的用法簡介Linux
- Linux下more命令高階用法Linux
- Linux下mv命令高階用法Linux
- Linux下crontab命令的用法:sudo crontab -lLinux
- Linux下 ls 命令的高階用法8例Linux
- echo命令在Unix中的作用以及其常見用法?
- linux echo命令的-n、-e兩個引數Linux
- linux 中 date命令的用法Linux
- linux useradd命令的基本用法Linux
- Linux sed命令用法Linux
- Linux 下使用 killall 命令終止程式的 8 大用法Linux
- rocky與linux的nmcli命令用法Linux
- Linux命令-Sed用法教程Linux
- Linux命令- echo、grep 、重定向、1>&2、2>&1的介紹Linux
- echo命令詳解
- 常用的Linux命令;Linux常用命令用法及實現方式Linux
- Linux——基礎命令用法(上)Linux
- Linux 命令列:find 的 26 個用法示例Linux命令列
- linux下mail的簡單用法LinuxAI
- Shell echo命令介紹
- Linux基礎命令---echo列印內容到標準輸出Linux
- Linux命令列:cURL的十種常見用法Linux命令列
- linux後臺執行命令:&與nohup的用法Linux
- linux中查詢find命令的複雜用法Linux
- linux下source命令Linux
- Linux下Ollama命令Linux
- linux下查詢命令的技巧Linux
- 命令列中常見命令用法及explainshell.com的用法命令列AI
- Linux常用命令用法及實現方式Linux
- Linux最常用的5個重啟命令基本用法講解!Linux
- Linux 命令列下的好東西Linux命令列
- Linux下 ln 命令詳解Linux
- Linux下常用命令Linux
- Linux 下 常見操作命令Linux
- linux下的命令列播放器mocLinux命令列播放器
- Linux下刪除資料夾的命令Linux
- Linux系統下強大的ethtool命令Linux