ウェブサイト検索

Linux の「echo」コマンドの 15 の実践例


echo コマンドは、Linux bash および C シェルで最も一般的かつ広く使用されている組み込みコマンドの 1 つであり、標準的なテキスト/文字列を表示するためにスクリプト言語やバッチ ファイルで通常使用されます。出力またはファイル。

echo コマンドの構文は次のとおりです。

echo [option(s)] [string(s)]

1. テキスト行を入力し、標準出力に表示します。

echo Tecmint is a community of Linux Nerds 

次のテキストを出力します。

Tecmint is a community of Linux Nerds 

2. 変数を宣言し、その値をエコーします。たとえば、変数 x を宣言し、その値 =10 を割り当てます。

x=10

その値をエコーします。

echo The value of variable x = $x 

The value of variable x = 10 

: Linux の「-e」オプションは、バックスラッシュでエスケープされた文字の解釈として機能します。

3. オプション「\b」の使用 - バックスラッシュ インタープリタ「-e」を使用してバックスペースを使用すると、間にあるすべてのスペースが削除されます。

echo -e "Tecmint \bis \ba \bcommunity \bof \bLinux \bNerds" 

TecmintisacommunityofLinuxNerds 

4. オプション「\n」の使用 – バックスペースインタープリタ「-e」を使用した改行は、使用されている場所から改行を扱います。

echo -e "Tecmint \nis \na \ncommunity \nof \nLinux \nNerds" 

Tecmint 
is 
a 
community 
of 
Linux 
Nerds 

5. オプション「\t」 – バックスペースインタープリタ「-e」を使用した水平タブを使用して、水平タブスペースを確保します。

echo -e "Tecmint \tis \ta \tcommunity \tof \tLinux \tNerds" 

Tecmint 	is 	a 	community 	of 	Linux 	Nerds 

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 を使用した垂直タブを使用して、垂直タブ スペースを確保します。

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 

: オプションを 2 回使用するか、必要に応じて何度でも、垂直タブ、水平タブ、および改行間隔を 2 倍にすることができます。

9. オプション '\r' の使用 – バックスペース インタープリタ '-e を使用したキャリッジ リターンで、出力にキャリッジ リターンを指定します。

echo -e "Tecmint \ris a community of Linux Nerds" 

is a community of Linux Nerds 

10. オプション '\c' を使用する – バックスペース インタープリタ '-e ' で末尾の改行を抑制し、改行を発行せずに続行します。

echo -e "Tecmint is a community \cof Linux Nerds" 

Tecmint is a community avi@tecmint:~$ 

11. オプション「-n」を使用して、末尾の改行のエコーを省略します。

echo -n "Tecmint is a community of Linux Nerds" 
Tecmint is a community of Linux Nerdsavi@tecmint:~/Documents$ 

12. オプション '\a' を使用する – バックスペース インタープリタ '-e ' を使用してアラートを返し、音声アラートを鳴らします。

echo -e "Tecmint is a community of \aLinux Nerds" 
Tecmint is a community of Linux Nerds

: 発射する前に、必ず音量キーを確認してください。

13. echo コマンド (ls コマンドの代替) を使用して、すべてのファイル/フォルダーを印刷します。

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 

14. 特定の種類のファイルを印刷します。たとえば、すべての「.jpeg」ファイルを印刷する場合は、次のコマンドを使用します。

echo *.jpeg 

network.jpeg 

15. リダイレクト演算子とともにエコーを使用すると、標準出力ではなくファイルに出力できます。

echo "Test Page" > testpage 

## Check Content
avi@tecmint:~$ cat testpage 
Test Page 
エコーオプション
 Options

説明

 -n

末尾の改行を出力しません。

 -e

バックスラッシュエスケープの解釈を有効にします。

 \b

バックスペース

 \\

バックスラッシュ

 \n

改行

 \r

キャリッジリターン

 \t

水平タブ

 \v

垂直タブ

現時点ではこれですべてです。以下のコメント欄で貴重なフィードバックをお寄せください。