60

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?

Anthony Geoghegan
  • 11,533
  • 5
  • 49
  • 56
jgr208
  • 2,896
  • 9
  • 36
  • 64

6 Answers6

77

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

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
47

but you can use

echo -e "Hi \n" 

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

wyanez
  • 471
  • 4
  • 3
  • \n to make a blank line is cleaner in the code. Better than having a bunch of echo "". It's a matter of choice, but the code looks cleaner. For places where you need a blank line before and after a line of text use "\n line of text \n" – Robin Jun 01 '22 at 04:25
10

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".

SLePort
  • 15,211
  • 3
  • 34
  • 44
10

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.

Raman Gupta
  • 1,580
  • 15
  • 12
4

printf

More portable and succinct. This prints a hundred lines.

shell

printf '\n%.0s' `seq 1 100`

bash

printf '\n%.0s' {1,100}
CervEd
  • 3,306
  • 28
  • 25
2

As John suggested use echo. However if you want to print a blank line followed by text and anther blank line - as in running a test then use echo -e suggested by wyanzes.

echo -e "\n Now we are going to load data \n"

puts a blank line before and after.

Sumit S
  • 516
  • 5
  • 17