HOWTO · Bash

在 Bash 中回顯新行

這顯示了使用 -e 和 $ 和 echo 命令列印新行的不同方法

本頁內容

本教程將展示使用 -e 和 $ 以及 echo 命令在 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 的新行。

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

輸出:

This is the first line
This is the second line