An article to understand Linux environment variables (local environment variables, global environment variables, persistent environment variables)

Article directory

  • 0 background
  • 1 Overview of environment variables
    • 1.1 Global environment variables
    • 1.2 Local environment variables
  • 2Create environment variables
    • 2.1 Local environment variables
    • 2.2 Global environment variables
  • 3 Delete environment variables
  • 4 Set the PATH environment variable
    • 4.1 Persistent environment variables
      • 4.1.1 Case 1: Default login shell
      • 4.1.2 Case 2: Interactive shell for non-login shell
      • 4.1.3 Case 3: Non-interactive shell for running scripts
    • 4.2 Recommendations for creating persistent environment variables
  • 5 data array

0 Background

The content of this article is summarized from “Linux Command Line and Shell Script Programming Encyclopedia”.

1 Overview of environment variables

Definition: Store information about a shell session and working environment in memory so that they can be easily accessed by programs or scripts running in the shell.

Global environment variables are visible to the shell session and all spawned subshells; local variables are visible only to the shell that created them.

1.1 Global environment variables

  • View method: env or printenv command.
  • To display the values of individual environment variables, you can use the printenv command, but do not use the env command.
  • You can use echo to display the value of a variable [When referencing an environment variable, you must add a dollar sign ($) in front of the variable. 】

1.2 Local environment variables

  • How to view: There is no command that only displays local environment variables in the Linux system. The set command will display all environment variables set for a specific process, including local variables, global variables and user-defined variables.

2Create environment variables

2.1 Local environment variables

my_variable=Hello

my_variable="Hello World"

echo $my_variable

Notice:

  • All environment variable names are in uppercase letters, which is standard convention for bash shells. If it is a local variable or shell script you created yourself, please use lowercase letters. Variable names are case-sensitive. Stick to lowercase letters when referring to user-defined local variables to avoid the potential disaster of redefining system environment variables.
  • There are no spaces between the variable name, the equal sign, and the value.
  • If the variable contains a string value with spaces, wrap it in double quotes.
  • The difference between single quotes, double quotes, and no quotes in commands:
    • Single quotes: Output the contents within the single quotes as they are, or describe what is seen in the single quotes as output;
    • Double quotes: If there are commands, variables, etc. in the content, the results of the variables and commands will be parsed first, and then the final content will be output.
    • No quotation marks: Strings containing spaces will not be treated as a whole output. If there are commands, variables, etc. in the content, the results of the variables and commands will be parsed first, and then the final content will be output. If there are spaces in the string If you wait for special characters, the complete output will not be possible.

2.2 Global environment variables

Creation method: First create a local environment variable, and then export it to the global environment. [The variables exported by the subshell export cannot be received by the parent shell]

Notice:

  • Modifying the global environment variable in the subshell will not affect the value of the variable in the parent shell.

3 Delete environment variables

Use the unset command to accomplish this. [When referencing environment variables in the unset command, remember not to use $. 】

Notice:

If you want to use variables, use $; if you want to operate variables, do not use $. An exception to this rule is using printenv to display the value of a variable.

4 Set PATH environment variable

The PATH environment variable defines the directory where commands and programs are searched.

Temporarily add the PATH environment variable:

PATH=$PATH:/home/christine/Scripts

Modifications to the PATH variable can only last until you exit or restart the system.

4.1 Persistent environment variables

When Linux starts the shell, environment variables are read from the startup file or environment file.

4.1.1 Case 1: Default login shell

  • /etc/profile
  • $HOME/.bash_profile
  • $HOME/.bashrc
  • $HOME/.bash_login
  • $HOME/.profile

The /etc/profile file is the main startup file of the default bash shell on the system. This startup file is executed when every user on the system logs in. The remaining four files [they are hidden files] provide a user-specific startup file to define the environment variables used by the user. Most Linux distributions only use one or two of these four startup files. The shell will run the first file found in the following order, and the rest will be ignored [$HOME represents a user’s home directory. It has the same effect as the tilde (~). 】:

$HOME/.bash_profile
$HOME/.bash_login
$HOME/.profile

There is no $HOME/.bashrc file in this list. This is because the file is usually run through other files.

The environment variables in the Ubuntu distribution are included in the /etc/bash.bashrc file. However, this file does not appear in the /etc/profile file of the CentOS distribution.

Notice:

  • Some Linux distributions use Pluggable Authentication Modules (PAM). In this case, PAM files are processed before the bash shell is started, and environment variables may be included in these files

4.1.2 Case 2: Interactive shell for non-login shell

Concept: The bash shell is not started when logging into the system (for example, it is started when typing bash at the command line prompt), then the shell you start is called an interactive shell.

If bash is started as an interactive shell, it will not access the /etc/profile file and will only check the .bashrc file in the user’s HOME directory.

The .bashrc file has two functions:

  • The first is to view the common bashrc file in the /etc directory.
  • The second is to provide users with a place to customize their own command aliases and private script functions (to be discussed in Chapter 17).

4.1.3 Case 3: Non-interactive shell for running scripts

Concept: This shell is used when the system executes shell scripts. The difference is that it doesn’t have a command line prompt.

The bash shell provides the BASH_ENV environment variable. When the shell starts a non-interactive shell process, it will check this environment variable to see the startup file to be executed. If there is a specified file, the shell will execute the commands in the file, which usually includes shell script variable settings.

If the BASH_ENV variable is not set, since some shell scripts are executed by starting a subshell, the subshell can inherit the variables exported by the parent shell. If the parent shell is a login shell, variables are set and exported in the /etc/profile, /etc/profile.d/*.sh, and $HOME/.bashrc files for child execution of the script. The shell can inherit these variables.

For scripts that do not start a subshell, the variables already exist in the current shell, so even if BASH_ENV is not set, the local variables and global variables of the current shell can be used.

Note: If the variables set but not exported by the parent shell are local variables, the subshell cannot inherit local variables.

4.2 Suggestions on creating persistent environment variables

For global environment variables (variables that all users in the Linux system need to use), you may prefer to place new or modified variable settings in the /etc/profile file, but if you upgrade the distribution you are using, This file will also be updated accordingly, and all your customized variable settings will be gone.

It is best to create a file ending with .sh in the /etc/profile.d directory. Place all new or modified global environment variable settings in this file.

In most distributions, the place where individual user-persistent bash shell variables are stored is in the $HOME/.bashrc file, which applies to all types of shell processes. But if the BASH_ENV variable is set, remember that unless it points to $HOME/.bashrc, you should put the non-interactive shell user variables elsewhere. The place.

5 data array

To reference a single array element, use square brackets + numeric index value [The index values of the environment variable array all start from zero]. To display the entire array variable, an asterisk can be used as a wildcard character in the position of the index value.

$ mytest=(one two three four five)
$ echo $mytest
one
$ echo ${mytest[2]}
three

The portability of array variables is not good. If you need to do a lot of scripting work in different shell environments, this will bring a lot of inconvenience.