在 Bash 中回显新行
Fumbani Banda
2023年1月30日
Bash
本教程将展示使用 -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
Enjoying our tutorials? Subscribe to DelftStack on YouTube to support us in creating more high-quality video guides. Subscribe
作者: Fumbani Banda
