いつかどこかで役に立つechoについて
9529 ワード
はじめに
echoコマンドについて調べたので備忘録がてらここに記しておく。対象はGNU版echoである。BSD版、及びSystem V版echoは試していない。
ちなみに、echoコマンドとは、与えられた文字列を標準出力に書き出すためのコマンドである。ほぼすべてのshellに組み込みコマンドとして入っているが、挙動は全く同一というわけではないので十分注意が必要なコマンドのひとつである。
シンタックス
シンタックスはこの通り。
echo [SHORT-OPTION]... [STRING]...
echoコマンドのオプション
Options | Description |
---|---|
-n | 行末の改行をしない |
-e | バックスラッシュでエスケープされた文字の解釈を有効にする |
\\ | バックスラッシュ |
\a | アラート |
\b | バックスペース |
\n | 改行 |
\r | 復帰 |
\t | 水平タブ |
\v | 垂直タブ |
\c | 行末の改行をしない |
次章からは、各オプションを使ったときの挙動について記す。
13個のechoコマンド出力例
1. 入力と出力
文字列の入力と標準出力を行う。
$ echo You are the Laughing Man, aren’t you?
You are the Laughing Man, aren’t you?
2. 変数
変数を定義し、その値をechoで出力する。
$ i=3
$ echo This is the "$i"rd time that we’ve met, yet you know who I am? Although they just call me that.
This is the 3rd time that we’ve met, yet you know who I am? Although they just call me that.
3. -e
と\a
オプション
アラートを発生させる。
$ echo -e "It’s quite audacious of you to come here. Why did you come \ahere?"
It’s quite audacious of you to come here. Why did you come here?
ボリュームの音量に注意しましょう。
4. -e
と\b
オプション
すべてのスペースを取り除く。
$ echo -e "I \bcame \bhere \bto \bbe \bsaved \bby \byou."
Icameheretobesavedbyyou.
5. -e
と\n
オプション
各単語の間に改行を挿入する。
$ echo -e "Don’t \nyou \nmean \nthe \nother \nway \naround?"
Don’t
you
mean
the
other
way
around?
6. -e
と\r
オプション
キャリッジリターンを挿入して改行する。
$ echo -e "True. But I’m so beat up right now that \rthis is the only method I can think of."
this is the only method I can think of.
7. -e
と\t
オプション
水平タブを挿入する。
$ echo -e "How \tselfish \tof \tyou."
How selfish of you.
8. -e
と\v
オプション
垂直タブを挿入する。
$ echo -e "I \vcan’t \vafford \vto \vlose \vany \vmore \vtime."
I
can’t
afford
to
lose
any
more
time.
9. -e
と\c
オプション
改行させないようにする。
$ echo -e "Well then, \cwhat would the Net’s hero want me to do?"
Well then, $
10. -n
オプション
改行させないようにする。
$ echo -n "I am going to tell you the truth behind the Laughing Man case."
I am going to tell you the truth behind the Laughing Man case.$
11. lsコマンド その1
lsコマンドの代替として使う。
$ echo *
of.txt psychoanalyst.txt Stekel.md the.txt Wilhelm.md Words.txt
12. lsコマンド その2
特定の種類のファイルのみ出力する。
$ echo *.md
Stekel.md Wilhelm.md
13. リダイレクト
リダイレクトしてファイルに文字列を書き出す。
$ echo "I will leave my memories behind. I am counting on you." > CAPTIVATED
$ cat CAPTIVATED
I will leave my memories behind. I am counting on you.
出典
欧州版 攻殻機動隊SAC Episode 19 "偽装網に抱かれて CAPTIVATED" より
Author And Source
この問題について(いつかどこかで役に立つechoについて), 我々は、より多くの情報をここで見つけました https://qiita.com/kentarosasaki/items/a4843c420f419807ea83著者帰属:元の著者の情報は、元のURLに含まれています。著作権は原作者に属する。
Content is automatically searched and collected through network algorithms . If there is a violation . Please contact us . We will adjust (correct author information ,or delete content ) as soon as possible .