Bash Shell uses a feature called environment variables to store information about shell sessions and the work environment.
Global environment variables
This is visible to both the shell session and all the spawned child shells. Local variables are visible only to the shell that created them.
System environment variables are basically uppercase to differentiate the environment variables of ordinary users.
To view global variables, you can use env or printenv
To view individual environment variables, use the PRINTENV command
$ printenv HOME/home/christine
You can also use the echo command to view the value of a variable, which refers to the value of an environment variable, which must precede the variable with a dollar sign ($), which allows the referenced variable to be used as a parameter to the command.
Global variables can be used for all child shells of the process.
Local Environment variables
This can only be seen in their process. Linux also defines the standard local environment variables by default. You can also define the environment variables, which are custom local variables by the user.
There are no commands in the Linux system that show only local environment variables. The SET command displays all environment variables that are set for a particular process, including local variables, global variables, and user-defined variables.
The difference between the Env, PRINTENV, and set commands is that the first two commands do not sort the variables, nor do they output local variables and user-defined variables.
Set user-defined variables set local user-defined variables
Echo $my _variable$ my_variable=echo $my _variable
Attention:
- If you want to assign a value to a variable string that contains a space, you must use single quotation marks, such as my_variable= "Hello World".
- There is no space between the variable name, equal sign, and value.
- If another shell is generated, it is not available in the child shell.
All environment variables are capitalized, which is the standard practice of the bash shell. If you are creating a local variable or a shell script, use lowercase letters.
Setting Global environment variables
The variable is visible in the child process created by the process that sets the global environment variable. The way to create an environment variable is to first create a local variable and then import it into a global variable.
$ my_variable="I am a Globalnow"echoecho $ my_ Variablei am a Global now$ exitexit
Modifying a Global environment variable in a child shell does not affect the value of a variable in the parent shell, and the child shell cannot even use the Export command to change the value of the Global environment variable in the parent shell, and the global environment variable in the parent shell still retains its original value.
Delete environment variables
Use the unset command to delete, and be careful not to use $.
$echoecho $my _variable$
If you want to use a variable, use $, if you want to manipulate the variable, do not use $. The exception to this rule is that Printenv displays the value of a variable.
When dealing with global environment variables, if you delete a global environment variable in a child process, this is only valid for the sub-process, as well as modifying the variable.
Setting the PATH environment variable
The PATH environment variable defines the directory used for command and program lookups, using: For partitioning, you simply reference the value of the original path and then add a new directory to the string.
Echo $PATH/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games$$ PATH= $PATH:/home/christine//usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/ Games:/usr/local/games:/home/christine/scripts
Note: If you want the child shell to be able to find the location of your program, be sure to export the modified path environment.
Note: Modifications to the PATH variable can only persist until the system is exited or restarted.
Using the Linux environment variables