Bash で改行をエコーする

Fumbani Banda 2023年1月30日 Bash
  1. Bash echo コマンド
  2. Bash で -e を使って新しい行をエコーする
  3. Bash で $ を使って改行をエコーする
Bash で改行をエコーする

このチュートリアルでは、echo コマンドで -e$ を使用して bash で改行を出力するさまざまな方法を示します。

Bash echo コマンド

Bash echo コマンドは、端末に出力を出力するために使用されるコマンドです。

echo 'I love working in Linux'

出力:

I love working in Linux

Bash で -e を使って新しい行をエコーする

echo コマンドは、デフォルトでは改行文字を認識しません。bash で改行を出力するには、-e オプションを追加して、echo コマンドが改行文字を解釈できるようにする必要があります。

echo を使用して -e オプションで改行を出力すると、すべてのシステムで機能するとは限りません。一部のシステムでは、-e オプションが無視される場合があります。新しい行を出力するためのより良い方法は、printf を使用することです。

echo 'This is the first line \nThis is the second line'

出力:

This is the first line \nThis is the second line
echo -e 'This is the first line \nThis is the second line'

出力:

This is the first line
This is the second line

Bash で $ を使って改行をエコーする

一重引用符で囲まれた改行文字の前に $ を使用して、エコー付きの改行を出力できます。

echo This is the first line$'\n'This is the second line

出力:

This is the first line
This is the second line
チュートリアルを楽しんでいますか? <a href="https://www.youtube.com/@delftstack/?sub_confirmation=1" style="color: #a94442; font-weight: bold; text-decoration: underline;">DelftStackをチャンネル登録</a> して、高品質な動画ガイドをさらに制作するためのサポートをお願いします。 Subscribe
著者: Fumbani Banda
Fumbani Banda avatar Fumbani Banda avatar

Fumbani is a tech enthusiast. He enjoys writing on Linux and Python as well as contributing to open-source projects.

LinkedIn GitHub