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:

  1. Variable Definition: Use the assignment operator = to define a variable. For example, my_var="hello" defines a variable named my_var and sets its value to "hello".
  2. Variable Reference: Use the $ symbol to reference the value of a variable. For example, echo $my_var outputs the value of the my_var variable.
  3. Variable Assignment: The assignment operator = can be used to modify the value of a variable. For example, my_var="world" changes the value of the my_var variable to "world".
  4. Variable Deletion: Use the unset command to delete a variable. For example, unset my_var deletes the variable named my_var.
  5. Variable Type: Variables in Bash are default strings but can be converted into other types using type conversion functions. For instance, use the declare command to define an integer variable, and use declare -a to define array variables, among others.
  6. 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 export command 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:

  1. $0: Represents the filename of the current script.
  2. $1, $2, $3...: Represent arguments passed to the script; $1 denotes the first argument, $2 the second, and so on.
  3. $#: Represents the number of arguments passed to the script.
  4. $@: Represents all arguments passed to the script, with each argument separated by spaces when expanded.
  5. $?: Represents the exit status code of the last command; 0 indicates successful execution, while other values indicate failure.
  6. $$: Represents the process ID (PID) of the current process.
  7. $USER: Represents the username of the current user.
  8. $HOME: Represents the path to the home directory of the current user.
  9. $PWD: Represents the path to the current working directory.
  10. $SHELL: Represents the path to the current shell.
  11. $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.