Google-foo is failing me. My question regards the actual Bash shell script itself. Is there a current or historical reason one would not want any blank lines in a shell script? Consider two versions of the following somewhat silly code:
#!/bin/bash
# Get the time
TIME=$(date " %H")
GREET=""
# Find the proper greeting
if [ $TIME -lt 12 ]; then
# Less than 12 means it's morning
GREET="Good morning"
elif [ $TIME -lt 18 ]]; then
# Less than 18 means it's afternoon
GREET="Good afternoon"
else
# Must be evening
GREET="Good evening"
fi
# Greet the user
echo "${GREET}"
Contrast that with this:
#!/bin/bash
#
# Get the time
TIME=$(date " %H")
GREET=""
#
# Find the proper greeting
if [ $TIME -lt 12 ]; then
# Less than 12 means it's morning
GREET="Good morning"
#
elif [ $TIME -lt 18 ]]; then
# Less than 18 means it's afternoon
GREET="Good afternoon"
#
else
# Must be evening
GREET="Good evening"
fi
#
# Greet the user
echo "${GREET}"
Notice how there are no blank lines at all in the second version? I've encountered code written by others (that have since moved on) that look like the second version. I'm wondering if there is any current or historical reason to do that?
Any time I try to search for "comment every line" or something similar, I get posts that talk about how to clear blank lines from other text files - not why one would want this in their bash script.
So again, is this a matter of personal taste? Or is there some reason, current or historical, where one would make sure there are no empty code lines at all?
CodePudding user response:
is this a matter of personal taste?
Yes.
is there some reason, current or historical, where one would make sure there are no empty code lines at all?
No.
The only reason I can think of, that sometimes I use, is that {
}
motions in VIM editor jump up to the first non-empty lines. So if you want to have visual separation, but want {
}
paragraph motions to skip over a whole block of file, you just put #
comment so that the line is not empty and is counted as part of paragraph. But, that makes sense to have lines with just #
in parts of the file, not the entire file.
CodePudding user response:
There is no current reason to do so.
You can enter as many blank lines as you wish. This will marginally increase the file size, but greatly aid readable (especially if combined with proper comments).
It may be that script that aim for low storage costs are used on devices with limited storage, meaning IoT and historical devices.