In Linux bash shell, semicolons in statements are generally used as code block identifiers.
1. A single-line statement generally uses semicolons to distinguish code blocks. For example:
Weblogic @ pmtest:/$ if ["$ PS1"]; then echo test is OK; fi
Test is OK
In this script or command line, two semicolons are required for the correct statement. The first Semicolon is the semicolon before then, used to identify the end Of the condition block, and the second semicolon before fi, identifies the end of the then block. If the two semicolons are missing, the program execution is incorrect.
It is interesting that the string after echo can be correctly identified without quotation marks.
Note: do not end with a semicolon.
2. If the code is written in multiple lines and the code block is differentiated by line breaks, a semicolon is not required. For example:
Weblogic @ pmtest:/$ if ["PS1"]
> Then echo "test is OK"
> Fi
Test is OK
From this example, we can see that if statements are divided into if blocks, then blocks, and fi end IDs. Of course, there may also be elesif blocks, such:
Weblogic @ pmtest:/$ if ["$ PS1"]
> Then echo test is OK
> Elif ["$ PS2"]
> Then echo here
> Fi
Test is OK
Note: here elif is used, but esle if is not used. if else if is used, it is an incomplete statement. The following example cannot be correctly executed:
Weblogic @ pmtest:/$ if ["$ PS1"]
> Then echo test is OK
> Else if ["$ PS2"]
> Then echo here
> Else
> Echo ""
> Fi
>
After you press enter, the shell program considers that the sentence is not complete and continues to wait for the input.