Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bash convention for if ; then

From this web page :

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

It's mentioned the usage of the if bracket then convention which need a space after the 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 is this coming from and if this is needed at all by certain shells?

like image 561
Chmouel Boudjnah Avatar asked Nov 02 '11 18:11

Chmouel Boudjnah


People also ask

What is if $? 0 ]; then?

$? is the exit status of the most recently-executed command; by convention, 0 means success and anything else indicates failure. That line is testing whether the grep command succeeded. The grep manpage states: The exit status is 0 if selected lines are found, and 1 if not found.

How do you write an if-else in a Bash script?

To add an if-else statement in your Bash script, start with the keyword “if” and specify the conditional expression with it. In the next line, add the “then” keyword and write out the statements you want to execute when the condition turns out true.

Can you have nested if statements in bash?

If statement and else statement could be nested in bash. The keyword “fi” indicates the end of the inner if statement and all if statement should end with the keyword “fi”. The “if then elif then else fi” example mentioned in above can be converted to the nested if as shown below.


4 Answers

This has become the style in the last few years:

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

However, back when dinosaurs like Burroughs and Sperry Rand ruled the earth, I learned to write if statements like this:

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

Then, you don't even need a semicolon.

The new style with then on the same line as the if started in order to emulate the way C and other programming languages did their if statements:

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

These programming languages put the opening curly brace on the same line as the if.

like image 73
David W. Avatar answered Nov 15 '22 20:11

David W.


Semicolon ; is an operator (not a keyword, like braces { }or a bang !) in Shell, so it doesn't need to be delimited with white space to be recognized in any POSIX-compliant shell.

However, doing so improves readability (for my taste).

Semicolon needs to be escaped if you mean a symbol "semicolon", not an operator.

like image 42
Roman Cheplyaka Avatar answered Nov 15 '22 19:11

Roman Cheplyaka


The space after the semicolon is not required by the syntax for any shell I know of, but it's good style and makes the code easier to read.

I suppose the "sometimes needs to be escaped" wording refers to cases like echo foo\;bar, where you don't want the semicolon to be interpreted as a separator by the shell.

like image 37
Keith Thompson Avatar answered Nov 15 '22 21:11

Keith Thompson


I do not believe that the space should be necessary there. There's nothing about requiring spaces in the POSIX sh spec.

Empirically, the following works fine in both bash 4.1.5(1) and dash:

$ if true;then echo hi;else echo bye;fi
hi
$ 
like image 33
Emil Sit Avatar answered Nov 15 '22 19:11

Emil Sit