Zeolite
Zeolite

Reputation: 105

Bash should I use ; in if statement and is there any difference?

can someone, please, explain is there any difference in two following examples?

  1. First example

    if [ command ]; then ... fi

  2. Second example

    if [ command ] then ... fi

Upvotes: 0

Views: 24

Answers (2)

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 798706

The second is an error. There must be a command separator between the command passed to if and the then clause. The command separator does not have to be a semicolon (a newline is sufficient), but there must be one.

Upvotes: 1

sschuberth
sschuberth

Reputation: 29821

Your second example does not work unless you put then into a new line. Using the semicolon allows you to put then into the same line as if. People argue which is more readable, it's just a matter of taste, no technical difference.

Upvotes: 1

Related Questions