environment variables, or global variables, exist with all the shells, and when you log into the system, you have the corresponding system-defined environment variables. The environment variables of Linux are inherited, that is, the child shell inherits the environment variables of the parent shell.
Local variables, variables in the current shell, it is clear that the local variables must contain environment variables. The non-environment variables of local variables of Linux are not inherited.
Files for environment variables in Linux
When you enter the system, Linux will read the environment variables for you, where the environment variables are stored, which is the file of the environment variables. There are many files in Linux that record environment variables, which are read in a certain order by the system.
1./etc/profile:
This file is a system environment variable that sets the environment information for each user and is executed when the user logs on for the first time. and collect the shell settings from the configuration file of the/ETC/PROFILE.D directory.
This file is a file that any user will read after logging into the operating system (if the user's shell is csh, tcsh, zsh, the file will not be read), used to obtain the system's environment variables, only read once when logging in.
Assuming that the user is using bash, then
2./ETC/BASHRC:
After the/etc/profile content is executed, if the user's shell is running bash, then the file is executed. In addition, each time a new bash shell is opened, the file is read.
Each user who uses bash executes the/etc/profile after logging in and executes the file when a new bash is opened. So, if you want each user who uses bash to do something for each new bash and every login, or to define some new environment variables, you can set it up in this.
3. ~/.bash_profile:
Each user can use the file to enter shell information that is specific to their own use. When the user logs on, the file is executed only once, by default, it sets some environment variables to execute the user's. bashrc file.
A single user The modification of this file will only affect every subsequent landing system. Therefore, it is possible to set a specific environment variable or special operation for a single user here, then it will fetch these new environment variables or do some special operations on each login, but only when landing.
4. ~/.BASHRC:
The file contains bash information dedicated to a single-person bash shell, which is read when logged in and every time a new shell is opened.
A single user The modification of this file will affect every subsequent landing system and a new bash for each time he opens. As a result, you can set a specific environment variable or special operation for a single user here, and each time it has a new login system or a new bash, it will get the corresponding special environment variables and special operations.
~/.bash_logout:
Executes the file each time it exits the system (exiting the bash shell).
Several commands
Env and Printenv
These two variables are used to print all environment variables:
Set
Used to display and set the current local variable. A single set shows all the variables of the current environment, which must include environment variables and some non-environment variables
unset
Used to clear variables. Whether the variable is an environment variable or a local variable, it can be cleared.
--The following is a clear local variable
[Email protected] oracle]$ Set|grep myname
Myname=ilonng
[Email protected] oracle]$ unset myname
[Email protected] oracle]$ Set|grep myname
--The following is a clear environment variable
[Email protected] oracle]$ Env|grep myname
Myname=ilonng
[Email protected] oracle]$ unset myname
[Email protected] oracle]$ Env|grep myname
Export
The environment variable that is used to turn the variable into the current shell and its child shell is the current shell and its child shell, so after re-landing, it sets the environmental variables to disappear. How do I make an environment variable permanent? Modify the configuration files for the environment variables described above
Source
When executing a script directly, it is actually running in a child shell environment, that is, a child shell is opened to execute the script, and the child shell exits automatically when the script finishes executing.
Is there a way to execute a script in the current shell? You can use the source command to have the script execute in the current shell. Such as:
[[email protected] nbu]$ Cat test.sh--View the contents of the script and display the contents of the variable
Echo $myname
[Email protected] nbu]$ echo $myname-variable exists, content is ilonng
Ilonng
[[Email protected] nbu]$ set |grep myname--variable is local variable
Myname=ilonng
[[Email protected] nbu]$ env |grep myname--variable is not an environment variable
[[Email protected] nbu]$ sh test.sh--direct execution, new open shell, non-environment variable local variable is not inherited, not visible in child shell
--There's nothing out here.
First, the shell variables are divided into local variables and environment variables.
1, local variables--in the user's existing running script to use
1) Define local variable format: variable-name=value
Example: [[email protected]/root]# localtest= "test"
[Email protected]/root]# echo $LOCALTEST test
2) display local variable format: set
Example: [[email protected] root]# set
3) Clear local variable format: unset variable-name
For example: [[email protected]/root]# unset localtest
Performing the Echo $LOCALTEST will not see the output of the variable localtest at this time.
2. Environment variables--used in all sub-processes
1) Define the environment variable format: Export Variable-name=value (more than one export keyword compared to the definition of a local variable)
Example: [[email protected]/root]# export domain= "chinaitlab.com"
[Email protected] Chinaitlab shell]# VI testenv.sh
#!/bin/bash
Echo $DOMAIN
[Email protected] shell]# chmod +x testenv.sh
[Email Protected]naitlab shell]#./testenv.sh
Chinaitlab.com
2) Display environment variable format: env (local variable display using set, environment variable display using env)
Example: [[email protected] test]# env
3) Clear environment variable format: unset variable-name (usage is the same as local variable, use unset)
Example: [[email protected] shell]# unset DOMAIN
At this point, the./testenv.sh will not see the output of the variable domain.
--------------------------------------------------------------------------------------------------------------- ------
The process of loading profile and BASHRC after user login is as follows:
1)/etc/profile-------->/etc/profile.d/*.sh
2) $HOME/.bash_profile--------> $HOME/.BASHRC---------->/ETC/BASHRC
Description
Bash executes the/etc/profile script first, and the/etc/profile script executes the/etc/profile.d/*.sh in turn first
Bash then executes the. bash_profile script in the user's home directory, and the. Bash_profile script executes the. BASHRC script under the user's home directory,
and the. BASHRC script executes the/ETC/BASHRC script
At this point, all the environment variables and initialization settings have been loaded and completed.
Bash then calls Terminfo and INPUTRC to complete the terminal properties and keyboard mapping settings.
Where path is a variable that is specifically explained:
If the superuser is logged in, path has been set to the following path before/etc/profile is executed:
/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
In the case of a normal user, path is set to the following path before/etc/profile executes:
/usr/local/bin:/bin:/usr/bin
Note here: When the user switches and loads variables, such as Su-, then, if the user switches themselves, such as the root user and then switch with Su-root, the loaded path and the above is not the same.
To be exact, it is not always the same. So, in the/etc/profile script, the following configuration is done:
If [' id-u ' = 0]; Then
Pathmunge/sbin
Pathmunge/usr/sbin
Pathmunge/usr/local/sbin
Fi
If the superuser is logged in, in the/etc/profile.d/krb5.sh script, add/usr/kerberos/sbin:/usr/kerberos/bin to the front of the path variable search route.
If you are a regular user, in the/etc/profile.d/krb5.sh script, add/usr/kerberos/bin to the front of the path variable search route.
In the/etc/profile script, the/usr/x11r6/bin directory is added at the end of the path variable
In $home/.bash_profile, the $home/bin directory is added at the end of the path variable
As an example of root user, the final path will be this (no other custom basis)
/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/usr/x11r6/ Bin:/root/bin
Take Alice User (normal user) as an example
/usr/kerberos/bin:/usr/bin:/bin:/usr/x11r6/bin:/home/alice/bin
--------------------------------------------------------------------------------------------------------------- -------------------
~/.bash_profile is read when the user logs on, and the command contained is executed
~/.BASHRC is read when the new shell is started and executes
~/.bash_logout Shell login is read at exit
Linux environment variable "Go"