bash 在 shell 脚本中回显空行的首选方法是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37052899/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 22:16:50  来源:igfitidea点击:

What is the preferred method to echo a blank line in a shell script?

bashshell

提问by jgr208

I am currently writing some code for a shell script that needs a blank line between two parts of the script, as thus they can be separated when output is displayed to the user.

我目前正在为 shell 脚本编写一些代码,该脚本的两个部分之间需要一个空行,因此在向用户显示输出时可以将它们分开。

My question is, I am not sure what the preferred practice is in a shell script for a blank line.

我的问题是,我不确定 shell 脚本中空行的首选做法是什么。

Is it preferred practice to just write echoand nothing else or to write echo " "as in echo with quotes and blank between the quotes?

是首选的做法是只写echo而不用别的,还是echo " "用引号和引号之间的空白来写?

回答by John Kugelman

echois preferred. echo " "outputs an unnecessary space character. echo ""would be better, but it's unnecessary.

echo是首选。echo " "输出一个不必要的空格字符。echo ""会更好,但这是不必要的。

回答by wyanez

but you can use

但你可以使用

echo -e "Hi \n" 

and print a blank line after Hi, with -e interprets the \n character.

并在 Hi 之后打印一个空行, -e 解释 \n 字符。

回答by SLePort

In its first implementation, echohad no option and outputs optional arguments ending with a new line, so it perfectly suit your needs.

在它的第一个实现中,echo没有选项并输出以新行结尾的可选参数,因此它非常适合您的需求。

For formatted outputs ending with a new line, printfis a better choice, for example : printf "%s\n\n" "output".

对于以新行结尾的格式化输出,printf是更好的选择,例如 : printf "%s\n\n" "output"

回答by Raman Gupta

All of these commands can be used to echo a blank line:

所有这些命令都可用于回显空白行:

echo, echo '', echo ""

echo, echo '',echo ""

We cant use echo "\n"or echo '\n'as they will give output as \nin both cases.

我们不能使用echo "\n"orecho '\n'因为它们会\n在两种情况下都提供输出。