Functions of semicolons in bash scripts

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.