空行を作成

空行を作成

現在ここに空の行を追加しようとしています。これまで使用したコマンドの例は次のとおりです。

echo -e "my kernel version is $(uname -a) \nand the current date is $(date)"

出力がこのようになるようにすべてをどのように変更しますか?

my kernel version is Linux centos7 3.10.0-1127.18.2.el7.x86_64 #1 SMP Sun Jul 26 15:27:06 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux
*** empty line***   
and the current date is Fri Apr 23 09:18:29 EEST 2021

ベストアンサー1

ここで文書を使用して複数行のメッセージが出力されます(ここに文書に空白行を含めると空白行が生成されます)。

cat <<END
my kernel version is $(uname -a)

and the current date is $(date)
END

printfこれはメッセージを出力するために使用されます(2つの連続した改行を出力して空行が生成されます)。

printf 'my kernel version is %s\n\nand the current date is %s\n' "$(uname -a)" "$(date)"

私のシステムでは、両方ともテキストを生成します。

my kernel version is OpenBSD box.prefix.example.com 6.9 GENERIC.MP#473 amd64

and the current date is Fri Apr 23 17:03:24 CEST 2021

また見なさい:なぜprintfがechoより優れているのですか?

おすすめ記事