Many programs and scripts use environment variables to obtain system information, store temporary data, and configure information.
6.1 What is an environment variable:
Bash Shell uses a feature called environment variable (environment variable) to store information about shell sessions and the work environment.
This feature allows data to be stored in memory so that the program can easily access them.
In the bash shell, there are two types of:
Global variables: Visible for Shell sessions and all spawned child shells
Local variables: visible only to the shell to which they are created
6.1.1 Global Environment variables
View global variables, $env $printenv
To view a global variable: $printenv HOME or
$echo $PATH//$ followed by the variable name. Allows variables to be used as command-line arguments
For example: LS $HOME equivalent to Ls/home/xiaochongyong
6.1.2 Local Environment variables
Visible only in the process in which they are defined.
Can be divided into: standard local variables and user-defined local variables
Set: Displays all environment variables (including global, standard local, custom local) set for a particular process, and also sorts alphabetically.
6.2 Set a custom variable. Case sensitive 6.2.1 Set local custom variables: try to use lowercase
$my _variable=hello
$echo $ my_variable
$my _variable= "Hello xcy"//two words so write, add "". = There is no space on the edge
$echo $ my_variable
In the parent shell, the child shell is not necessarily available. and vice versa.
6.2.1 set Global environment variables.
The variable is visible in the child process created by the process that sets the global variable.
To create a method:
(1)
$my _variable=hello//Create environment variable
$export my_variable//export to global variables
(2) Export $my _variable=new_hello//This is the change value
The child shell cannot be changed to the value of the global variable in the parent shell. Valid only in the current child shell.
6.3 Deleting environment variables
$unset my_variable
The child shell is removed and cannot be reflected in the parent shell.
6.4 The default shell environment variable.
By default, the bash shell uses specific environment variables to define the system environment.
These variables are already set up on the Linux system.
6.5 Setting the PATH environment variable
PATH: Defines the directory used for command and program lookups.
The Echo $PATH directory is separated by colons.
You can add a new search directory to an existing PATH environment variable. Like what:
GCC Hello.c-o xcy
Generally only./xcy Run
But after adding the environment variable, you can run it.
But switching to a different directory is still not possible. Unless you add
Modifications to path can only continue until the system is exited or restarted, and cannot persist.
6.6 Positioning System Environment variables
When the login system launches a bash shell, bash looks for commands in several files by default. This file is called a startup file or environment file.
The startup file that bash checks depends on how you start the bash shell. There are three ways to start:
1. Login Shell: When logged into the system, the Bash shell is launched as a login shell. Commands are read from 5 boot files.
(1)/etc/profile (system default master boot file, each user login on the system will execute this boot file)
(2) home/.bash_profile HOME/.BASHRC home/.bash_login home/.profile
These 4 are user-specific and provide a user-specific startup file to define the environment variables used by the user.
Most Linux distributions use only one or two of these four files. Not all files are available for each user.
Shell, run the first found file in the following order, and the rest is ignored:
Home/.bash_profile
Home/.bash_login
Home/.profile
Notice there is no HOME/.BASHRC.
HOME/.BASHRC This file is usually run through other files.
There's only home/.profile in My computer.
The red section indicates that checking the home directory is not a. BASHRC, if any, will be executed first. BASHRC inside the order
2. Interactive shell process
Not started when the system is logged on, such as running bash directly under the terminal. This is the interactive shell process.
This process does not access the/etc/profile and only checks the. BASHRC in the home.
The. BASHRC has two functions: 1. View common BASHRC files in/etc directory, 2. Provides a place for users to customize their command aliases and private script functions
3. non-interactive shell
This is the shell used by the system to execute shell scripts.
There is no command line prompt.
Bash shell provides bash_env environment variables. When a non-interactive shell is started, bash_env is checked to see the startup file to be executed. If so, the shell executes the commands inside. This typically includes shell scripting variable settings.
My computer does not have a bash_env value. How is it handled? By inheriting the exported variables of the parent shell.
For example: The parent shell is the login shell. Environment variables are set up and exported in the/etc/profile/etc/profile.d/*.sh HOME/.BASHRC file, and the child shell used to execute the script can inherit these variables. 、
A variable that is not exported by the parent shell is a local variable, and the child shell cannot inherit.
6.6.4 environment variable Persistence
For global environment variables,/etc/profile will not be there after the upgrade. You can create a file at the end of the. SH in the/ETC/PROFILE.D directory and place all new or modified global variables in this file
The place where personal persistent bash shells are stored is HOME/.BASHRC.
For example, the previous command alias, if placed here can be used forever.
6.7 Array variables.
The environment variable can be an array (starting at 0). Like what:
This is seldom used.
"Linux command line and Shell Scripting Encyclopedia" chapter Sixth environment variables