Bash Basic Syntax

Source: Internet
Author: User

the basic syntax of a shell
Assignments generally take the following form: variable name = string
1. "=" number can not be a space on either side, or it will be wrong. (This beginner is particularly prone to mistakes)
2. If there is nothing after "=" in the assignment statement, the variable is an empty string, and if it is declared but not assigned, the variable defaults to an empty string.
3. If a variable contains spaces, tabs, line breaks, you should enclose them in double quotes, or you will get an error.
4. In a shell program file, if you want to refer to a variable that has already been defined, you typically add a "$" symbol to the variable name, which means to tell the shell, followed by a variable.
5. Single quotation mark (' ... '): Single quotation marks are also referred to as strong references, referencing all content. In single quotes, no character has a special meaning.
6. Double quotes ("..."): double quotes are also known as weak references, except for 3 metacharacters (dollar Signs), ' (inverted quotes), and/(backslash) references to all content. In double quotes, these 3 characters also retain their special meanings.
7. Inverted quotation mark (' ... '): command substitution, command substitution allows embedding a command in a command. The shell first executes the embedded command and replaces the command with the output. The shell then executes the entire command.
8. In the shell variable reference, a combination of a variable and a long string, if the current variable is at the end of the string, you can take advantage of the direct reference; If you are in the middle or the beginning, you can include the variable name in curly braces. As follows:
Program:
[C-sharp] View plaincopy
#!/bin/bash
Address=beijing
Echo $address
Echo ${address}test
Echo test$address

Output:

Beijing
Beijingtest
Testbeijing

wildcard characters in the shell (for pattern matching)
1. "*" Symbol
The "*" symbol is used to match characters that occur 0 or more times in a string, such as: s* can match shells, Shanghai, and so on. When using the "*" symbol, be aware that when matching the filename to the pathname, "." The symbol and "/" must display a match, such as: *test can not match the ". Httest" file, but with ". *test" to match, while "/home/test" need to use "/*/test" to match.
2. "?" Symbol
“?” The symbol matches only one character of the corresponding position. For example: M?ke can match "Mike", "make" and so on, but cannot match "Mooke".
3. "[]" symbol
The function of the title is to match any one of the characters within the bounds of that character Group. The characters in square brackets can consist of characters that are directly out, such as: [ADEHK], or can be composed of the starting and terminating characters that represent the bounds and the connection character "-" in the middle. such as: [A-za-h], [0-9] and so on.
4. "!" Symbol
“!” Symbols are used in conjunction with the "[]" symbol, "!" The function is to match characters that are not listed in square brackets. For example: t[!a-h]st, you can represent Tyst, t9st, but you cannot represent test.


Input in the shell

The input in the shell is implemented by the function read, which is the prototype: Read variable 1 [variable 2]
The Read function allows you to interactively assign a value to a variable, and of course you can assign a value to a variable by tabs or spaces, as described below:
1. If there are more variables than the number of strings in the input string, then assign the value, leaving the variable null.
2. If the number of variables equals the number of strings in the input string, one by one corresponds to the assignment.
3. If the number of variables is less than the number of strings in the input string, just in addition to the ordinal assignment, the last variable to accept the remaining string.
For example:
[C-sharp] View plaincopy
#!/bin/bash
echo "Input your name and age:"
Read Name Age
echo "Your name is:" $name
echo "Your Age is:" $age

If you enter Jim 15, the output:

Input your name and age:
Your name Is:jim
Your age is:15

the output in the shell
The output in the shell is implemented by the Echo function, and echo can directly output the value of the variable following it or directly output the string following it. The Echo function is separated by a space and ends with a newline character. If you want to keep more than one space between the data, enclose them in double quotes so that the shell can manipulate them correctly. Another: The Echo function also defines a set of escape characters to include the "-e" option when using escape characters. The escape characters are as follows:
"\a": Bell Alarm, "\b": Back one character, "\f": Change page, "\ n": Show line break, "\ T": tab, "\v": Vertical tab, "\ r": return character, "\ \": Backslash.
For example:
[C-sharp] View plaincopy
#!/bin/bash
echo-e Hello, ' \ n ' world!
echo Hello, ' \ n ' world!
Echo '-e ' hello, ' \ n ' world!
echo-e Hello, "\ n" world!
ECHO-E hello,\nworld!
Echo hello,\nworld!

Output:

Hello
world!
hello,/nworld!
Hello
world!
Hello
world!
hello,nworld!
hello,nworld!

Arrays in the shell
The shell supports one-dimensional arrays, but it does not limit the size of the array, which starts with the array subscript 0.
When manipulating an array, the value is: ${array name [subscript]}; array name [subscript]= value (assigned to a single array element); If you want to assign a value to all the elements of the entire array, you can use: array name = (value 1, value 2, value 3, ...), separated by a space between values and values.
The traversal array is also available in addition to the loop: array name [*] or array name [@], for example:
[C-sharp] View plaincopy
#!/bin/bash
Array1[0]=beijing
Array1[1]=shanghai
Array2= (Guangzhou,shenzhen,chengdu)
echo "/${array1[0]} =" ${array1[0]}
echo "/$array 1[1] =" $array 1[1]
echo "/${array1[*]} =" ${array1[*]}
echo "/$array 1 =" $array 1
echo "/${array2[*]} =" ${array2[*]}
echo "/${array2[@]} =" ${array2[@]}
echo "/$array 2 =" $array 2
echo "/$array 2[1] =" $array 2[1]

Output:

${array1[0]} = Beijing
$array 1[1] = beijing[1]
${array1[*]} = Beijing Shanghai
$array 1 = Beijing
${array2[*]} = Guangzhou,shenzhen,chengdu
${array2[@]} = Guangzhou,shenzhen,chengdu
$array 2 = Guangzhou,shenzhen,chengdu
$array 2[1] = guangzhou,shenzhen,chengdu[1]
Note: Array assignment and output are a bit cumbersome.
For an array modification operation, you can reassign it again, but if you want to delete an already assigned element, you need to use an external command: unset, such as: unset array[0] Clears the element with subscript 0, at which point the array size is reduced by one; Unset array[@] You can empty all elements of an entire array element. For example:
[C-sharp] View plaincopy
#!/bin/bash
Address= (Beijing,shanghai,shandong)
Address[0]=nanjing
Echo ${address[*]}
Unset Address[0]
Echo ${address[*]}

Output:
Beijing Shanghai Shandong
Shanghai Shandong
Note: about the array input and output rules more, more practice to master


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.