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
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.
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.