Shell Variable Basics
The shell variable is a very "weak" variable, by default a variable holds a string, and the shell does not care what the string means. So for mathematical operations, you must use some commands such as let, declare, expr, double brackets, and so on. Shell variables can be divided into two categories: local variables and environment variables. Local variables are only available in the shell in which they are created. Environment variables can be used in the shell in which they are created and in any child processes it derives from. Some variables are created by the user, while others are private shell variables. The variable name must begin with a letter or underscore character. The remaining characters can be letters, numbers (0~9), or underscore characters. Any other character marks the end of the variable name. The name is case sensitive. When assigning a value to a variable, you cannot have any whitespace around the equals sign. To assign a null value to a variable, you can follow a newline character after the equals sign. With the SET command you can see all the variables, the unset var command clears the variable Var,var equivalent to not defined. readonly var can change Var to a read-only variable, and you cannot make any changes to Var after the definition. There are many references to shell variables, which can easily be used to get the value of the shell variable, the length of the variable value, a string of variables, the value of the variable being partially replaced, and so on.
Local Variables : scope for the entire bash process
Set Varname=value
Local Variables : Scope is the current code snippet
Local Varname=value
environment variable: scope is the current shell process and its child processes;
Export Varname=value
Varname=value
Export VARNAME
Positional variables :
$, $, ...
Special variables:
$ A: The file name of the current script
$num: Num is a number starting from 1, and $ is the first argument, and $ is the second argument, ${10} is the tenth argument
$#: Number of arguments passed in the script
$*: All positional parameters (as a single string)
[Email protected]: all positional parameters (each as a separate string).
$?: The return value of the previous command in the current shell process, if the previous command succeeds, the value of $ is 0, otherwise a value other than 0 is commonly used to make an if statement condition
$$: pid of the current shell process
$!: PID of the last process running in the background
$-: Shows the current options used by the shell
$_: Last parameter of previous command
Marco Learning notes--shell variable types