Linux environment variables

Source: Internet
Author: User
Tags echo command

Linux environment variables

Global variables and local environment variables:

Bash shell uses a feature called environment variable to store information about shell sessions and work environments. This is also the origin of the environment variable name. This feature also allows us to store data in the memory so that we can access them in programs or scripts running in shell. Bash shell has two types of environment variables: global variables and local variables.

Global environment variables are visible in shell sessions and any sub-processes generated by the shell. Local variables are only visible in the shell that creates them. In the application that generates child processes that require parent process information, this reflects the important role of global variables. System environment variables use uppercase letters to distinguish them from normal user environment variables. We use the printenv command to view global environment variables. To view the value of a global variable, we can use the echo command. However, when referencing an environmental variable name, we need to add a dollar sign, such as echo $ HOME, to view the HOME directory. No command can only display local environment variables. We can use the set command to display all environment variable sets of a specific process, which also includes global environment variables. That is to say, all the global variables we see in the printenv command appear in the output of the set command.

Set local environment variables:

We can create local variables visible in shell processes. We can assign a value or a string to the environment variable by using the equal sign to specify the variable as a specific value, such as test = xin, then we can use echo $ test to output the variable value. If we want to specify a string value containing spaces, We Need To enclose it with single quotation marks to specify the starting position of the character. If we do not use single quotes, bash shell assumes that the next character is another command.

In bash shell, we recommend that you use lowercase letters if you create a new environment variable. It helps us distinguish between personal and system environment variables.

Note that there must be no space between the environment variable name, equal sign, and value. This is very important. If we add any space in it, bash shell interprets the value as a separate command.

After we set the local variable, we can use it anywhere in the shell process. However, if another shell is generated, it cannot be used in the sub-shell. We can use exit to exit the sub-shell and return it to the parent shell, of course, we can use bash to enter a sub-shell.

Set global environment variables:

Global environment variables are visible to any sub-processes created by processes that set global environment variables. To create a global environment variable, create a local environment variable and use export to place it everywhere in the global environment. For example, we first> test = xin, and then> export test.

The export Command makes it global. When exporting local environment variables, we do not need to use dollar signs to reference the variable name.

Remove environment variables:

We use unset to remove environment variables. The operation example is unset test. Note that the dollar symbol is not used here. When operating global environment variables, if we use unset to remove global environment variables in the child process, this operation is only valid for the child process, and the global environment variables are still available in the parent process.

Default shell environment variables:

(1) CDPATH: a colon-separated directory list, used as the search path for the cd command

(2) HOME current user's HOME directory

(3) IFS is used to split the Character List of fields, and shell uses them to split text strings

(4) the file name of the current user's mailbox in MAIL. For new emails, bash shell checks the file

(5) MAILPATH multiple file names in the current user's mailbox, separated by colons. For new emails, bash shell checks each file in the list.

(6) value of the last option parameter processed by the OPTARG getopts command

(7) index value of the last option parameter processed by the OPTIND getopts command

(8) List of directories separated by colons in PATH. shell will search for commands in these directories.

(9) PS1 main shell command line interface prompt string

(10) PS2 shell command line interface prompt string

Set the PATH environment variable:

(1) Note: The PATH also displays the shell SEARCH Command Order.

(2) For example, If We append a directory to the PATH, we can use the following format example: PATH = $ PATH:/home/xin/test

(3) We append the directories of some commands to the PATH environment variable. We can execute our program anywhere in the virtual directory structure.

(4) A common technique we use is to include a dot symbol in our PATH environment variable, which represents the current directory.

Locate system environment variables:

When you log on to Linux to start bash shell, bash checks some files by default to execute commands. These files are called Startup files. Bash processes the Startup File Based on the bash shell startup method. We can use three methods to start bash shell: ① as the default logon shell at login. ② As an interactive shell for non-Logon shell. ③ Run the script as a non-interactive shell.

Log on to shell:

Log on to the shell and find four different startup files to process the current commands:/etc/profile, $ HOME/. bash_profile, $ HOME/. bash_login, $ HOME/. profile

The/etc/profile file is the main default startup file of bash shell on the system. Every user on the system will execute this startup file upon logon, the other three startup files are specific to each user and can be customized according to their needs. When we log on to the Linux operating system, bash will execute the command in the/etc/profile Startup File. We also noticed that many of the export variables in this file are global variables.

Profile files also use another technical feature, that is, the ability to iterate/etc/profile. the for statement of any file in the d directory, which enables the Linux system to provide a STARTUP file specific to the application. These files will be executed by shell upon login.

The three files in the $ HOME Directory have the same functions. They provide user-specific startup files for defining user-specific environment variables, most Linux distributions use one of the three methods. bash_profile or. bash_login or. profile. Note that all three of them start with a dot, indicating that they are hidden files and will not be displayed in the common ls command list.

Interactive shell:

If you have started a bash shell without logging on to the system, for example, if you only type bash in the cli prompt, then we start an interactive shell. The interactive shell is different from the login shell, however, it still provides a cli prompt for us to enter the command.

If bash is started as an interactive shell, it does not process the/etc/profile file. Instead, it checks the. bashrc file in the user's HOME directory. The. bashrc file executes two tasks: ① first, check the public bashrc file in the/etc directory. ② Provide a place for users to enter their personal aliases and private script functions.

The public/etc/bashrc file is run by anyone who starts the Interactive shell Session on the system. Some environment variables are set in the default file, but they are not set to global by using the export command. Remember that the interactive shell Startup file is run every time you start a new interactive shell. Therefore, any sub-shell will automatically execute the Interactive shell Startup File. We will also notice that the/etc/bashrc file also runs the application-specific Startup file located in the/etc/profile. d directory.

Non-interactive shell:

It is the shell in which the system starts to execute shell scripts. The difference is that you don't have to worry about cli prompts, but you still want to run specific startup commands every time you start the script in the system.

Bash shell provides the BASH_ENV environment variable. When the shell starts a non-interactive shell process, it checks the name of the Startup file in the environment variable. If the variable has a value, shell will execute the command in the file.

Variable array:

An excellent feature of environment variables is that they can be used as arrays. arrays are variables that can store multiple values. Values in an array can be used separately or referenced as a whole.

To set multiple values for an environment variable, we only need to list them in parentheses. The values are separated by spaces, for example,> xin = (one two three four five) however, when we use> echo $ xin, only the first value of the array is displayed.

If we want to reference a single array element, we must use a numerical index value, which indicates the position of the element in the array. The index value is placed in square brackets, for example: echo $ {xin [2]}. Note that the index value of the environment variable array starts from 0. Of course, we can also use the asterisk wildcard in brackets to view all values, such as echo $ {xin [*]}.

We can also change the value of an index location, such as xin [2] = star. Of course, we can also use unset to remove a value from the array, such as unset xin [2]. of course, we can also use unset xin to remove the entire array.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.