Shell whitespace issues

Source: Internet
Author: User

1. When defining variables, You cannot leave spaces on either side of the = sign.

eg
Gender=femal------------Right
Gender =femal-----------Wrong
gender= femal-----------Wrong
2. Conditional test statement [the symbol is left blank on both sides .]

eg

if [$gender = Femal]; Then-------right.
echo "You are Femal";
Fi

if[$gender ...-----------------------wrong
If [$gender ...----------------------wrong.

3 Conditional test content, if it is a string comparison, the comparison symbol on both sides to leave a space!

eg

if [$gender = Femal]; Then-------right.
if [$gender = Femal]; Then--------wrong.
If [$gender =femal]; Then---------wrong.

4. If if and then are written on the same line, then, notice that then the front should be followed; No problem if then the line is written. (Bash:==,dash =)

eg

if [$gender = = Femal]; Then-------right.
if [$gender = = Femal]
Then-------------------------------right.
if [$gender = Femal] then-------wrong. Then the front is less; No..
Prompt error message:
Syntax error near unexpected tokens then
In the same vein, there are many error messages, such as
Syntax error near unexpected token fi, etc. are caused by this.

5.if must keep up with then. The same elif must follow then.

Otherwise, an error message is indicated:
Syntax error near unexpected token else

1) The If statement is followed by then, with a semicolon in front;
2) The space is very important, the shell will consider a space before the command, if A=3 is considered an assignment operation, if written as a = 3, then will think a is a command this= ' ls-l |grep ' ^-' | Wc-l '
3) The operators should be separated by a space, such as test! -D $, where! And-D will be separated by a space
Whitespace is an important delimiter in command parsing

Shell whitespace issues

Related Article

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.