Types Annotate
strong variables Variables must be declared before use, and even
weak variables need to be initialized Variables are declared when they are used, even without distinguishing between types
The role of variables: used to save changed data
The variable name is fixed, set by the system or defined by the user
Variable values vary according to user settings and system environment changes
Setting method
variable name = variable value
Types of Shell Variables
Type description
Environment variables are maintained by the system and are used to set the working environment, only individual environment variables can be directly changed by users
Positional variables pass arguments to script programs via the command line
Predefined variables A type of special-purpose variable built into bash that can be called directly, but cannot be directly assigned or modified
Custom variables are set, modified and used by users themselves
environment variable
At system startup, load the variables defined by the system configuration file
Variable names and variable values are system settings
Variable names are usually defined with capital letters
The value stored in the variable is specified. Usually, it is not recommended to modify the value of the system environment variable.
The scope is the current shell process and its subprocesses
configuration file
Profile comments /etc/ profile ~/.bashrc_profile
Related operations
env lists all environment variables set lists all variables
Common environment variables
PWD
PATH
USER
LOGNAME
UID
SHELL
HOME
PS1
PS2
How to view variable values
echo $ variablename
position variable: read-only variable
position variable read-only variable When executing a script or shell, pass a value to a script or function $ 1 .....$n is represented as $n, where n is a positive integer ${10}......${N}
Predefined variables (special variables)
predefined variables
The predefined variables in the shell used by the current script
Variable names are fixed, and variable values are usually not modified
variable name
variable name meaning $$ The PID number of the currently running process $ ? The return status value after the last command is executed, 0 means normal, 1 or other values are abnormal $ 0 currently running process or script name $# number of position variables $ * treats all command line parameters as a single string, equivalent to " $1$2$3 " $ ! The PID number of the last process in the background $@ treats each command line argument as a separate string, equivalent to " $1 " , " $2 " , " $3 "
Exercise: Write a script
Pass two parameters ( integers ) to the script ;
Display the sum and product of the two
#!/bin/bash # if [ $# -lt 2 ]; then echo "Usage: cacl.sh ARG1 ARG2" exit 8 be echo "The sum is: $[$1+$2]." echo "The prod is: $[$1*$2]."
Exit status and what it means:
Exit status and its meaning Comment 0 means the operation was successful, the program execution did not encounter any problems 1 ~ 125 means the operation failed, script command, system command error or parameter passing error 126 The command was found but could not be executed 127 The command to run was not found > 128 command is forcibly ended by the system 1 , 2 , 127 reserved by the system
Difficulty Theory and Practical Analysis
Difference between $* and $@ $ *: Treat all command line parameters as a single string, equivalent to " $1$2$3 " $@: Treat each command line parameter as a separate string, equivalent to " $1 " , " $2 " , " $3 " . This is the best way to pass parameters to other programs because it preserves any whitespace embedded in each parameter.
Custom variables (local variables)