Bash convention for if; then

From this web page:

http://tldp.org/LDP/abs/html/abs-guide.html

He mentioned using an if bracket, and then a convention that needs a space after a semicolon:

;

Command separator [semicolon]. Permits putting two or more commands on the same line.

echo hello; echo there


if [ -x "$filename" ]; then    #  Note the space after the semicolon.
#+                   ^^
  echo "File $filename exists."; cp $filename $filename.bak
else   #                       ^^
  echo "File $filename not found."; touch $filename
fi; echo "File test complete."

Note that the ";" sometimes needs to be escaped.

Does anyone know where this comes from, and if at all necessary for some shells?

+5
source share
5 answers

This has become a style over the past few years:

if [ -x "$filename" ]; then
   echo "hi"
fi

However, when dinosaurs such as Burroughs and Sperry Rand ruled the earth, I learned to write if such statements were as follows:

if [ -x "$filename" ]
then
    echo "hi"
fi

Then you don’t even need a semicolon.

then , if, , C , if:

if (! strcmp("foo", "bar")) {
   printf "Strings equal\n";
}

, if.

+9

; - ( , { } bang !) , , POSIX- .

, ( ).

, " ", .

+5

, , .

, " " , echo foo\;bar, , .

0

, . , POSIX sh spec.

bash 4.1.5 (1), :

$ if true;then echo hi;else echo bye;fi
hi
$ 
0

, . , c.u.s., .

0

All Articles