Concept
Origins
The environment variables in Linux originated from the design requirements of early UNIX systems. Through environment variables, users and processes can dynamically influence system behavior without modifying the system’s core code. This flexibility and configurability make environment variables a key part of Linux system and application configurations.
In UNIX, each process has its own environment (i.e., a set of environment variables), which are passed from the parent process to the child process at startup. The most typical example is when a user logs into the system, the Shell process started by the system inherits a set of default environment variables, such as the user’s home directory, Shell type, etc. Users can modify or add environment variables based on this, affecting the behavior of programs or processes they start.
Concept Corrections
Some books and articles may divide environment variables into global environment variables and local environment variables, but this classification is inaccurate,
because there is no definition of these two categories in official documentation, not even in English names.
Therefore, to avoid logical problems in semantics, this article will not classify environment variables as above.
《Linux Command Line and Shell Scripting Bible》
The section on environment variables in this book is problematic, not a translation issue, but the original version has problems.
Printing Env Vars
Command: printenv
Without arguments, it prints all environment variables.
Prints a specific environment variable.
Command: env
Without arguments, it prints all environment variables.
Command: echo
Prints a specified variable, which can be an environment variable or a Shell variable.
Customizing Shell Vars
Defines a Shell variable named soda with the value green.
printenv
Outputs nothing, because soda is not an environment variable, it’s just a Shell variable.
Command Group
Modify in Command Group
Subshell modifications do not affect external data.
Creating a Bash Instance
Outputs nothing, which is the basic scope of a regular Shell variable.
Customizing Env Vars
Environment variables can be created by exporting Shell variables using the export
command, as defined in the official manual.
Variables can be defined and exported simultaneously, or separately.
printenv
After being converted to an environment variable, printenv can normally print it.
Command Group
Modify in Command Group
Similarly, modifying environment variables within a subshell does not affect external data.
Creating a Bash Instance
Environment variables are accessible in a new Bash instance.
Get All Export Variables
Without arguments, or using -p
, all export variables can be printed.
Cancel Export Env Vars
Using the -n
option can cancel export, making it a regular Shell variable again.
printenv has no output, echo outputs normally, because soda is no longer an environment variable.
Deleting Variables
Below will delete the entire variable, whether it’s an environment variable or a Shell variable.
Command: declare
Used to declare variables and attributes. If no names are given, then display the values of variables instead.
Common parameters:
-i
:Declares the variable as an integer.-r
:Declares the variable as read-only.-x
:Exports the variable as an environment variable.-p
:Displays the declaration and current value of the variable.
Without Arguments
The effect is the same as a regular Shell variable.
Display Var Declaration
Display All Vars Declaration
Declare Env Vars
The definition of soda can also be seen in export
.
Diff Between -x and –
It’s not hard to see that the declaration symbols can be used to distinguish different types of variables.
-x
:Declaration of environment variables.--
:Declaration of regular Shell variables.
After canceling export.
Built-in Cmd Manual
Some built-in commands cannot be viewed using man
, but can be viewed using the help
command or the --help
option.
export
declare
Same as above
unset
Same as above