directory

  1. Basic commands
  1. variable
  1. Create and run the script
  1. conditions
  1. cycle
  1. function
  1. Others (read, mktemp, trap)

variable

The environment variable

  1. The commandenvPrint the environment variables of the system
$ env
Copy the code

The user variables

  1. The commandsetPrints the user variables of the system
$ set
Copy the code
  1. The commandset name valueOr directly usename=valueYou can set environment variables. In this command, the name of the variable is on the left of the equal sign, and the variable is on the right of the equal sign.
$a="z" $b="a string" $b="a string" $e="$(ls -l foo.txt)" # $e="$(ls -l foo.txt)" # $e="$(ls -l foo.txt)" # $f="$((5 * 7))" $f="$((5 * 7))Copy the code

Note that there can be no Spaces on either side of the equals sign, and if the value of the variable contains Spaces, the value must be enclosed in quotes.

  1. The commandunset nameOr directly usename=""User variables can be deleted
$ a="z"
$ echo "${a}"
$ unset a
$ echo "${a}"
Copy the code
  1. The commandexport name=valueYou can set environment variables that can be inherited to subshells.

Note: Environment variables created by set are only available to the current Shell; by default, subshells cannot read variables defined by the parent Shell.

$export foo=bar # Create subshell $bash # Read $foo $echo $foo bar # Modify the variable $foo =baz # Exit subshell $exit # Read $foo $echo $foo barCopy the code

Special variables

  1. $0Is the name of the current Shell (when executed directly on the command line) or the name of the script (when executed in a script).
$ echo "${0}"
-bash
Copy the code
  1. $?Is the exit code of the previous command, used to check whether the previous command is executed successfully. If the value is zero, the previous command is successfully executed. If the value is non-zero, the previous command fails to be executed.
$ls notexistsfile ls: cannot access notexistsfile: No Such file or directory $echo "{$? $ls. XXXXXX XXXX XXXX XXX $echo "{$? 0}"Copy the code

String processing

In scripting, you often encounter the need for string processing, such as path processing. Here are some common string processing operations.

  1. Get string length
$varname="Good Morning" $echo "${#varname}"Copy the code
  1. Get a substring
$varNAME ="Good Morning" $echo "${varName :0:2}" ${varname:0:2}"Copy the code
  1. Case conversion
$varname="Good Morning" echo "${varname^^}" Good Morning! ${varname,,} echo "${varname,,}" good morning!Copy the code
  1. Search and replace
  • use${variable#pattern}forThe shortest matchAnd returns the remainder, or the original string if the match is unsuccessful.
  • use${variable##pattern}forLongest match (Greedy match)And returns the remainder, or the original string if the match is unsuccessful.
$ txtPath="/home/yanx/pink.txt"
​
$ echo "${txtPath#/*/}"
yanx/pink.txt
​
$ echo "${txtPath##/*/}"
pink.txt
Copy the code
  • use${variable/pattern/string}forLongest match (Greedy match)And replace the first matching part withstring, or the original string if an unsuccessful match.
  • use${variable//pattern/string}forLongest match (Greedy match)And replace all matching parts withstring, or the original string if an unsuccessful match.
$ txtPath="/home/yanx/yanx.txt"
​
$ echo "${txtPath/yanx/yanX}"
/home/yanX/yanx.txt
​
$ echo "${txtPath//yanx/yanX}"
/home/yanX/yanX.txt
Copy the code

The matching pattern does not follow the regular expression completely. You can only use *,? , [], etc.

Expression processing

$foo="$((2*2))"; $foo="$((3 > 2))"Copy the code

Array processing

  1. Create an array

Arrays can be created by assigning values one by one.

ARRAY[INDEX]=value
Copy the code

In the syntax above, ARRAY is the name of an ARRAY, which can be any valid variable name. INDEX is an integer greater than or equal to zero. It can also be an arithmetic expression. Notice that the first element of the array has an index of 0, not 1.

Here are three ways to create an array:

ARRAY[0]=value1 ARRAY[1]=value2 ARRAY[2]=value3 ... ARRAY[N]=valueN ARRAY=(value1 value2 ... ARRAY=(value1, value2, value3)Copy the code
  1. Store the data

The purpose of array is to store valid data. There are two ways to store data, one is to store the data input by the user, the other is to store the result of command execution.

The following commands can be used to store user data:

read -a dices
for dice in ${dices[@]};
do
    echo ${dice}
done
Copy the code

You can use the following command to read the result of the command execution:

dices=$(cat ~/.bash_profile)
for dice in ${dices[@]};
do
    echo ${dice}
done
Copy the code

Note: The loop operation can refer to the following section [Loop structure]. When using array loop, only the first data is returned if only dices is used, only the first data is returned if {dices} is used, only the first data is returned if dices is used, and only all data is returned if {dices[@]} is used.

  1. Read the data

The loop reads are already in the above example, and the following example is a single read:

$$dices = (1, 2, 3, 4, 5)echo ${array[0]}
1
Copy the code
  1. An array of operating
  • Get the length of the
${#array[*]}
${#array[@]}
Copy the code
  • Gets the index that has valid values
array[0]=0 array[2]=2 for item in ${! array[@]} do echo ${item} doneCopy the code
  • Delete array element and delete array
array[0]=0
array[1]=1
array[2]=2

Print the full array contents
for item in ${array[@]}
do
    echo ${item}
done

Delete the element at index position 1 and print 1, 2
unset array[1]
for item in ${array[@]}
do
    echo ${item}
done

Delete the entire array and print the contents empty
unset array
for item in ${array[@]}
do
    echo ${item}
done

Copy the code