What is the preferred method to echo a blank line

2019-04-04 13:03发布

问题:

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.

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

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

回答1:

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



回答2:

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

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



回答3:

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

echo, echo '', echo ""

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



标签: bash shell