A small gift for beginners of bash

Source: Internet
Author: User
A small gift for beginners of bash-Linux general technology-Linux programming and kernel information. The following is a detailed description. Some common mistakes in bash programming come from abs-guide
Note !!!! All the examples here are incorrect !!!

===== Variable usage ====
1. Define the bash reserved string as a variable name/variable name containing reserved characters

Case = 123 # case is a reserved string

Ca (= 123 # (Reserved characters)

Var-1 = 123 #-is reserved characters, var_1 is good

2. The variable starts with a number.

12var = 123.

3. Duplicate variables and function definitions

Do_something ()

{

Echo "This function does something with \" $1 \"."

}

Do_something = do_something

Do_something


4. Variable assignment = space on both sides

Var1 = 123 # var1 = 123


===== Compare [] ====
5. Space is missing in []


[$ Var-eq 5]

[$ Var = AB]


6. <> do not add \


[$ Var <AB] # in this case, <> is interpreted as output redirection, and [$ var \ <AB] is


7. =\> \ <与 -eq -gt -lt不分


[$ Var-eq 45.6] #=\> \ <用来比较字串,而eq gt lt只能用来比较整数,注意是整数,不是数值


8. The sha-bang script cannot contain invalid characters. The content written in dos/win is wrapped in \ r \ n. The actual number of lines in the script is #! /Bin/bash \ r \ n, but only #! /Bin/bash \ n is valid

9. the operation on the variable in the sub-shell cannot be passed to its parent shell.
Var = 123.

($ Var = 456)

Echo $ var # The result is 123 instead of 456.

Echo 456 | read var

Echo $ var # The result is still 123. The pipeline will issue a sub-shell.
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.