Bash Variable Classification and Characteristics
Variables in Bash primarily fall into three categories: user-defined variables, environment variables, and predefined variables.
User-Defined Variables
The distinction between user-defined variables and other types of variables lies in the fact that they are valid only within the current Bash shell by default as local variables. Below are keywords related to user-defined variables:
- Variable Definition: Use the assignment operator
=to define a variable. For example,my_var="hello"defines a variable namedmy_varand sets its value to "hello". - Variable Reference: Use the
$symbol to reference the value of a variable. For example,echo $my_varoutputs the value of themy_varvariable. - Variable Assignment: The assignment operator
=can be used to modify the value of a variable. For example,my_var="world"changes the value of themy_varvariable to "world". - Variable Deletion: Use the
unsetcommand to delete a variable. For example,unset my_vardeletes the variable namedmy_var. - Variable Type: Variables in Bash are default strings but can be converted into other types using type conversion functions. For instance, use the
declarecommand to define an integer variable, and usedeclare -ato define array variables, among others. - Variable Scope: User-defined variables are local by default and valid only within the current Bash shell. If you want a variable to be accessible in both the current Bash shell and all its child processes, use the
exportcommand to export it as an environment variable.
Note that there are also special variables in Bash such as $0, $1, $2, etc., which store information like script names and arguments; these differ from user-defined variables.
Environment Variables
Environment variables in Bash can be accessed by the entire operating system and all processes.
Bash includes many predefined environment variables. Here are some common ones:
| Variable | Description |
|---|---|
PATH |
Specifies a list of directories searched when looking for executable files. |
HOME |
Specifies the path to the current user's home directory. |
USER |
Specifies the username of the current user. |
LOGNAME |
Specifies the login name of the current user. |
SHELL |
Specifies the path to the current shell. |
PS1 |
Controls how the Shell command prompt is displayed. |
LANG |
Specifies the default language for the system. |
LC_ALL |
Overrides the LANG environment variable, specifying the default language and locale settings for the system. |
PWD |
Specifies the path to the current working directory. |
TERM |
Specifies the type of terminal currently in use. |
DISPLAY |
Specifies the address of the X server display. |
EDITOR |
Specifies the default text editor for the system. |
HOSTNAME |
Specifies the name of the current host machine. |
HISTSIZE |
Specifies the maximum number of commands saved in Shell history records. |
You can use the printenv command to list all environment variables. For example, the following command lists all environment variables:
printenv
You can use the echo command to view the value of a specific environment variable. For instance, the following command outputs the value of the PATH environment variable:
echo $PATH
Predefined Variables
Bash contains many predefined variables that store special values to facilitate Shell script writing and execution. Here are some common ones:
$0: Represents the filename of the current script.$1,$2,$3...: Represent arguments passed to the script;$1denotes the first argument,$2the second, and so on.$#: Represents the number of arguments passed to the script.$@: Represents all arguments passed to the script, with each argument separated by spaces when expanded.$?: Represents the exit status code of the last command;0indicates successful execution, while other values indicate failure.$$: Represents the process ID (PID) of the current process.$USER: Represents the username of the current user.$HOME: Represents the path to the home directory of the current user.$PWD: Represents the path to the current working directory.$SHELL: Represents the path to the current shell.$IFS: Represents the Internal Field Separator for the Shell, typically consisting of spaces, tabs, and newlines.
These predefined variables are extremely useful in Shell scripts; referencing their values can help execute specific operations within a script. For example, you can use $0 to retrieve the name of the script, $1 to get the first argument passed by the user, or $? to determine whether the last command executed successfully.
Note that the values of these variables are typically read-only and cannot be modified directly. If modification is required, special Shell commands and syntax must be used. For example, you can use the shift command to move script argument positions, thereby modifying the values of parameter variables like $1, $2, etc.