shell variables and quotes

Variables

Variables do not need to be declared, and initialization does not need to specify a type.
Variable naming
1: Cannot use keywords (reserved words) in the program
2: Only numbers, letters and underscores can be used, and cannot start with numbers
3: It is recommended that commands be easy to understand To
display the variable value, use the echo command, add $variable name, or use ${variable name},
for example: echo $JAVA_HOME
or echo ${JAVA_HOME}

variable classification


Local variables, environment variables, local variables, location variables, special variables

Local Variables

are only valid for the current shell process, and are invalid for subprocesses of the current process and other shell processes.
Definition: VAR_NAME=VALUE
Variable reference: ${VAR_NAME}
Cancel variable: unset VAR_NAME
is equivalent to a private variable (private) in java, which can only be used by the current class, and cannot be used by subclasses and other classes.

Environment variable
'' single quotes do not parse variables
"" double quotes will parse variables
`` Backticks are executed and referenced to the execution result of a command, similar to $(...)


custom environment variables for the current shell process and its children Valid for shell processes, invalid for other shell processes
Definition : export VAR_NAME=VALUE
is valid for all shell processes and needs to be configured in the configuration file
vi /etc/profile
source /etc/profile
is equivalent to the protected modifier in java, and can be shared by the current class, descendant classes, and the same package.

A local variable
is called in a function, and when the function is executed, the variable disappears. It is valid for a code fragment
in the shell script.
Definition: local VAR_NAME=VALUE

is equivalent to a variable defined in a method in the java code, and is only valid for this method.

Positional variables
$1,$2,.....${10}....
test.sh 3 89
$0: the script itself
$1: the first parameter of the
script $2: the second parameter of the script
Equivalent to the main function in java In the args parameter, you can get external parameters.

Special variable
$?: Receive the return status code of the previous command The
return is between 0-255
$#: Number
of parameters $*: Or $@: All parameters
$$: Get the process ID (PID) of the current shell ( Can achieve script suicide) (or use the exit command to exit directly or use exit [num])

single quotes, double quotes, backticks
'' single quotes do not parse variables
"" double quotes will parse variables
`` backticks are executed and quoted The execution result of a command, similar to $(...)

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326258565&siteId=291194637