Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Any reason not to have blank lines of code in Bash script (historical or otherwise)

Tags:

bash

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?

like image 424
Chris Parker Avatar asked Nov 19 '25 19:11

Chris Parker


1 Answers

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.

like image 65
KamilCuk Avatar answered Nov 22 '25 10:11

KamilCuk



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!