Linux基礎程式設計 echo命令

2021-10-07 15:44:06 字數 3072 閱讀 8275

06總結

echo命令用於在shell中列印shell變數的值,或者直接輸出指定的字串。linux的echo命令,在shell程式設計中極為常用, 在終端下列印變數value的時候也是常常用到的,因此有必要了解下echo的用法echo命令的功能是在顯示器上顯示一段文字,一般起到乙個提示的作用。

格式:echo[option]..

.[string]..

.

-n 不輸出行尾的換行符.

-e 允許對下面列出的加反斜線轉義的字元進行解釋.

-e 禁止對在strings中的那些序列進行解釋.

--help 顯示幫助並退出(須單獨執行)

--version 輸出版本資訊並退出(須單獨執行)

\nnn  字元的ascii**為nnn

(八進位制)

\\ 反斜線

\a 報警符(bel)

\b 退格符

\c 禁止尾隨的換行符

\f 換頁符

\n 換行符

\r 回車符

\t 水平製表符

\v 縱向製表符

5.1 輸出字串
[deng@localhost ~

]$ echo "hello itcast"

hello itcast

[deng@localhost ~

]$

5.2 輸出變數path
[deng@localhost ~

]$ echo $path

/usr/local/bin:

/usr/bin:

/usr/local/sbin:

/usr/sbin:

/home/deng/

.local/bin:

/home/deng/bin

[deng@localhost ~

]$

5.3 轉義特殊字元
[deng@localhost ~

]$ echo \$path

$path

[deng@localhost ~

]$

5.4 重定向到檔案中
[deng@localhost ~

]$ echo "hello itcast"

> txt

[deng@localhost ~

]$ cat txt

hello itcast

[deng@localhost ~

]$

5.5 輸出命令結果
[deng@localhost ~

]$ echo `date`

2023年 06月 30日 星期二 19:23

:27 cst

[deng@localhost ~

]$ date

2023年 06月 30日 星期二 19:23

:30 cst

[deng@localhost ~

]$

5.6 輸出換行符
[deng@localhost ~

]$ echo -e "a\nb\nc"ab

c[deng@localhost ~

]$

5.7 輸出退格符
[deng@localhost ~

]$ echo -e "123\b456"

12456

[deng@localhost ~

]$

5.8 輸出字串不換行
[deng@localhost ~

]$ echo -n "hello itcast"

hello itcast[deng@localhost ~

]$

5.9 支援萬用字元
[deng@localhost ~

]$ echo *

.ctest.c

[deng@localhost ~

]$

5.10 指定輸出顏色
[deng@localhost ~

]$ echo -e "\033[1;31mthis is red color\033[0m"

this is red color

[deng@localhost ~

]$

或者

[deng@localhost ~

]$ echo -e "\e[1;31mthis is red color\e[0m"

this is red color

[deng@localhost ~

]$

第二種方式解釋下:

5.11 設定背景色
[deng@localhost ~

]$ echo -e "\e[1;42mgreen background\e[0m"

green background

[deng@localhost ~

]$

ps:顏色碼:重置=0,黑色=40,紅色=41,綠色=42,黃色=43,藍色=44,洋紅=45,青色=46,白色=47

5.12 檔案閃爍

[deng@localhost ~

]$ echo -e "\033[37;31;5mlinux server stop...\033[39;49;0m"

linux server stop...

[deng@localhost ~

]$

ps:紅色數字處還有其他數字引數:0 關閉所有屬性、1 設定高亮度(加粗)、4 下劃線、5 閃爍、7 反顯、8 消隱

以上便是命令echo的基本用法了,很多都用不到,用得最多的就是echo "hello world"此種形式。

目前參加工作以來,並沒有在linux平台上面工作,所以,對於一些命令,只是找了一些用法,然後貼出來,以後接觸linux工作的時候直接翻自己部落格就好。

linux基礎命令 cat和echo

一 cat cat命令是linux下的乙個文字輸出命令,通常是用於 某個檔案的內容的 cat主要有三大功能 1.一次顯示整個檔案。catfilename 2.從鍵盤建立乙個檔案。cat filename 只能建立新檔案,不能編輯已有檔案.3.將幾個檔案合併為乙個檔案。cat file1 file2 ...

shell程式設計之echo命令

shell的echo命令是用於字串的輸出,格式為 echo string 1.顯示普通字串 echo it is test 結果為 it is a test2.顯示轉義字串 echo it is a test 結果為 it is a test 3.顯示變數 bin bash read name ec...

linux中的echo命令

承接上一章所介紹的command line 這裡我們用echo這個命令加以進一步說明。標準的command line包含三個部件 command name options argument echo是乙個非常簡單 直接的linux命令 將argument送出至標準輸出 stdout 通常就是在顯示器...