HOWTO · Bash

Bash で改行をエコーする

これは、echo コマンドで-e と$を使用して新しい行を出力するさまざまな方法を示しています

このページの内容

このチュートリアルでは、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