First, the name of the variable
The variable name must begin with a letter or underscore, consisting of a letter, number, or underscore, and the variable name cannot exceed 255 characters in length.
Ii. Classification of variables
1. User-defined variables
2. Environment variables
3. Positional parameter variables
4. Pre-defined variables
Third, user-defined variables
A user-defined variable is a "local variable" that can only take effect in the current shell
1. Defining variables
Variable name = variable Value
Example:
Note: There can be no spaces before and after the equals sign, otherwise the following error message appears:
2. Variable invocation
$ variable name or ${variable name}
Example:
3. Variable Overlay
Example:
Or:
4. Deleting variables
unset variable Name
Example:
5. View variables
1) Env: View all environment variables
2) Set: View all variables (including user-defined variables)
IV. Environment variables
The environment variable is a "global variable" that takes effect in the current shell and all child shells of the shell.
1. Setting Environment variables
Export variable name = variable Value
Or
Variable name = variable Value
Export variable Name
Example:
Note: When setting environment variables, it is recommended to use uppercase to avoid conflicts with user-defined variables or system commands (both lowercase).
2. Delete environment variable, environment variable call, view environment variable
User-defined variables are used the same way
3. Setting PS1 Environment variables
The PS1 environment variable affects user prompt information, as shown in the following example:
Five, Position variable 1. $n
N is a number, and $ A represents the command itself, $1-$9 represents the first to the Nineth parameter, and more than 10 of the parameters need to be enclosed in curly braces, such as ${10}.
Example:
#!/bin/Basha=$1b=$2sum=$ (($a +$b) echo $sum
2. $*
This variable represents all the arguments in the command line, $* all the parameters as a whole
#!/bin/bashfor in"$*"do echo $i done#运行: Test. SH 1 2 3 #输出: 1 2 3 #解释:all parameters in $ * are considered as a whole, so this for loop only loops once
3. [Email protected]
This variable also represents all parameters in the command line, but [email protected] treats each parameter differently
#!/bin/bash"[email protected]"echo $iDone
#运行: Test. 3#输出:
1 2 3 #解释: Each parameter in [email protected] is considered to be independent, so this for loop loops multiple times
4. $#
This variable represents the number of all parameters in the command line
#!/bin/bashecho $# #运行: Test. SH 1 2 3 #输出: 3
Six, the predefined variable 1. $?
The return status of the last command executed. If this value is 0, the previous command was executed correctly, and if the value is not 0, the error code that was executed by the previous command.
Example:
2. $$
Process number (PID) of the current process
Example:
3. $!
Process number (PID) of the last process running in the background
#!/bin/bashfind"test.sh" &Echo $!
VII. READ command
Accept keyboard Input
1. Parameter description
-P "prompt message": The output prompt when waiting for read input
-T number of seconds: the Read command waits for user input, using this parameter to specify the wait time
-N Character number: The Read command accepts only the specified number of characters and continues execution
-S: Hide input data, apply to confidential information input
2. Example
#!/bin/Bashread-P"Please input username:"Usernameread-P"Please input password:"-s PasswordEcho-E"\ n"Read-P"Please input sex[f/m]:"-N1SexEcho-E"\ n"Echo "Welcome $username [$sex]"
Shell Learning notes-Shell variables