The use of the ()

A subshell

() Open the subshell, the outer shell is not affected

#! /bin/bashpwd (cd /etc; pwd) pwdCopy the code

Output: /home/atvoid/ document/my nut cloud /project/ShellLife /etc / /home/atvoid/ document/my nut cloud /project/ShellLife

Execute command $()

Equivalent to ‘ ‘, but $() is recommended

A =$(echo 123) echo $aCopy the code

Initialize an array/dictionary

#! /bin/bash
array=(a b c d)
echo ${array[*]}

declare  -A dict=([k1]="v1" [k2]="v2")
echo ${dict[*]}
Copy the code

Output a, B, C, D, v1, v2

The use of the []

Internal symbols such as test are not commonly used

The only comparison operators available in test and [] are == and! =, both are used for string comparison, not integer comparison, integer comparison can only use the form -eq, -gt. “Ab” and “BC” : [ab \< BC], the result is true. [] : [ab \< BC] : [ab \< BC] : [ab \< BC]

  1. You must add a space to the right of the open parenthesis and to the left of the close parenthesis, otherwise an error will be reported.
  2. The test command uses standard mathematical comparison symbols to represent string comparisons and text symbols to represent numeric comparisons. A lot of people get it backwards. If used backwards, the shell might not get the right result.
  3. Greater than or less than symbols must be escaped, otherwise they are interpreted as redirects.
#! /bin/basha=1 b=2 if [ $a -lt $b ]; Then echo a="a" b="b" if [$a!= $b]; Then echo A is not equal to b fiCopy the code

other

Reference array elements array[1] re [0-9]

(()) and [[]] are commonly used

These are enhanced versions of [] for mathematical comparison expressions and string expressions, respectively. In (()), there is no need to escape the size of the expression from the symbol. In addition to using standard mathematical operators, the following symbols can be added:

(()) and $(())

  1. Integer extension. this extension calculation is an integer calculation and does not support floating point.((exp))Structure extends and evaluates the value of an arithmetic expression. If the expression results in 0, the exit status code returned is 1, or “false “, while an expression with a non-zero value returns 0, or “true”. The expression exp is 1 if it is true and 0 if it is false.
  2. As long as the operators and expressions in parentheses comply with the C language operation rules, they can be usedIn $((exp))Medium, or even trinary operators. When you do different carry operations (binary, octal, hexadecimal), the output is all automatically converted to decimal. Such as:echo $((16#5f))The result is 95 (16 to decimal)
  3. Simple to use(())Variable values can also be redefined, for examplea=5; ((a++))$a can be redefined to 6
  4. Often used to compare arithmetic operations. Variables in double parentheses may not be used$Symbol prefix. Multiple expressions in parentheses can be separated by commas. As long as the expression in parentheses conforms to the C language operation rules, for example, can be used directlyfor((i=0; i<5; i++)), if no double parentheses are usedfor i in $(seq 0 4)orfor i in {0.. 4}. If can be used directly againif (($i<5)), if no double parentheses are usedif [ $i -lt 5 ].
#! /bin/bash
#Redefine variablesA =5 ((a++)) echo $a #
#forcyclefor ((i = 0; i < 5; i++)); Do echo Current id: $I done
#conditionalif ((1 + 2)); Then echo true fi if! ((1, 1)); Then the echo false fi
#Arithmetic operationEcho $((1 + 2 * 3)Copy the code

[[]]

  1. [[Is a key of the bash programming language. It’s not a command,[[]]Structure than[]The structure is more versatile. All characters between [[and]] do not have filename extension or word splitting, but argument extension and command substitution.
  2. Pattern matching of strings is supported, and shell regular expressions are even supported when using the =~ operator. String comparisons can use the one on the right as a pattern, not just a string, than[[hello == hell?]], the result is true.[[]]Matches a string or wildcard character, without quotes.
  3. use[[...]]Conditional judgment structure, not[...].Can prevent many logic errors in scripts. For example, &&, | |, <, and > operator can exist in normal[[]]Conditional judgment structure, but if it occurs in[]Structure, will report an error. For example, you can use it directlyif [[ $a != 1 && $a != 2 ]], if no double parentheses are usedif [ $a -ne 1] && [ $a != 2 ]orif [ $a -ne 1 -a $a != 2 ].
  4. Bash treats the expression in the double braces as a single element and returns an exit status code.
#! /bin/bashif [[ hello == hell? ]]; then echo 'hello == hell? true' fi if [[ 5 > 4 ]]; then echo '5 > 4 true' fi if [[ 123 =~ ^[0-9]+ ]]; then echo '123 match ^[0-9]+' fiCopy the code

The use of the {}

Variable substitution ${}

#! /bin/bash
array=(a b c d)
#  echo $array[*] will print a[*]Echo ${array[*]} # echo ${array[*]} #Copy the code

The special replacement

  • ${var:-string}/${var:=string}

    ${var:-string} = ${var:-string} = ${var:-string} = ${var:-string} = ${var:-string} ${var:=string} ${var:=string} ${var:=string} ${var:=string} Var. ${var:=string}; var. ${var:=string};

  • ${var:+string}

    If var is not empty, it is not replaced by the value of the variable var (null). (Since the variable var is null, the two statements are equivalent.)

  • ${var:? string}

    If var is not empty, replace ${var:? String}; If the var variable is empty, string is printed to standard error and exits from the script. We can use this feature to check that the value of a variable is set.

String extraction and replacement

  • ${var:num}

    From num to end,0 counts

  • ${var:num1:num2}

    Starting with num1, extract num2 and count 0

  • ${var/pattern/pattern}

    Replace the first one

  • ${var//pattern/pattern}

    All replacement

var="01234560"
echo $var #01234560

echo ${var:5} #560

echo ${var: -6} #234560

echo ${var:(-6)} #234560

echo ${var:1:4} #1234

echo ${var/0/-1} #-11234560

echo ${var//0/-1} #-1123456-1
Copy the code

Four patterns match the replacement structure

# is to remove the left side (on the keyboard # to the left of $)

% is removed to the right (on the keyboard % is to the right of $)

A single symbol in # and % is the minimum match, and two identical symbols are the maximum match.

  • ${var#pattern}

    ${variable#pattern} in this pattern, the shell looks in the variable to see if it starts with the given pattern. If so, the shell removes from the command line the contents of the variable with the shortest pattern on the left

  • ${var##pattern}

    ${variable##pattern} the shell looks in the variable to see if it ends in a pattern. If so, the shell removes the longest pattern from the variable from the command line

  • ${var%pattern}

    ${variable%pattern}, the shell looks in the variable to see if it ends in the pattern given by the variable. If it does, the shell removes the following pattern from the command line

  • ${var%%pattern}

    ${variable%%pattern}, the shell looks in the variable to see if it has a pattern ending. If it does, the shell removes the longest pattern from the variable from the command line

var=testcase echo $var #testcase echo ${var%s*e} #testca echo ${var%%s*e} #te echo ${var#? e} #stcase echo ${var##? E} # stcase echo $} {var# # * e # empty echo $} {var# # * s echo ${var# # test} # # e caseCopy the code

Said the scope of

touch {a.. d}.txt

The result is a.txt b.txt c.txt d.txt