Sinsing on the environment variables in Linux

Source: Internet
Author: User
Tags echo command

Global variables and local environment variables:

The bash shell uses a feature called environment variables to store information about the shell session and the work environment, which is also the name of the environment variable. This feature also allows us to store data in memory for easy access to programs or scripts that run in the shell. The bash shell has two types of environment variables: global variables and local variables.

Global environment variables are visible in the shell session and in any child processes that the shell produces. Local variables are only visible in the shell in which they are created. This represents the important role of global variables in applications that produce child processes that require the information of the parent process. System environment variables are used with all uppercase letters to distinguish them from normal user environment variables. We use the PRINTENV command to view Global environment variables. To see the value of a global variable, we can use the echo command, but when referencing an environment variable name, we need to add a dollar sign, such as Echo $HOME, to check the housekeeping directory. There are no commands to display only local environment variables, and we can use the SET command to display all the set of environment variables for a particular process, including global environment variables. This means that all of the global variables that our printenv command sees appear in the output of the set command.

Set Local environment variables:

We can create local variables that are visible in the shell process. We can assign a numeric value or a string to an environment variable by specifying the variable as a specific value using the equals sign, such as Test=xin, and then we can output the variable value using the Echo $test. If we want to specify a string value that contains spaces, we need to use single quotation marks to enclose the starting position of the specified character. If we do not use single quotes, the bash shell assumes that the next character is another command.

One of the conventions in our bash shell is that if you create a new environment variable, we recommend using lowercase letters. It helps us distinguish between personal environment variables and system environment variables.

It is important to note that there can be no space between the environment variable name, equal sign, and value, and if we add any spaces to it, the 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 created, it cannot be used in a child shell, we can exit the child shell with exit, return to the parent shell, and of course we can use bash to enter a child shell.

to set Global environment variables:

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

The export command here makes it global, and when exporting local environment variables, we do not have to use the dollar sign to refer to the name of the variable.


to Remove an environment variable:

We use unset to remove the environment variables, the operation example unset test, and note that the dollar sign is not used here. When manipulating global environment variables, if we use unset to remove global environment variables in a child process, the operation is only valid for the child process, and the global environment variable is still available in the parent process.


Default shell environment variables:

(1) Cdpath Colon-delimited list of directories to use 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 the shell uses them to split the text string

(4) Mail the file name of the current user's mailbox, and for new messages, bash shell will check the file

(5) Mailpath multiple file names for the current user's mailbox, separated by colons, and for new messages, bash Shell examines each file in the list

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

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

(8) PATH Colon-delimited list of directories in which the shell looks for commands

(9) PS1 main shell command line interface prompt string

(10) PS2 shell command line interface prompt string


Set the PATH environment variable:

(1) Note: Path also shows the order of the shell lookup commands

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

(3) We append the directory of certain commands to the PATH environment variable, and we can execute our program anywhere in the virtual directory structure.

(4) One of the techniques we often use is to include a point symbol in our PATH environment variable, which represents the current directory.


Positioning System environment variables:

By logging on to the Linux system to launch the Bash shell, bash will by default check some files to execute commands, which are known as startup files. The startup file that bash handles relies on the method of launching the bash shell, and there are three ways to start bash Shell:① as the default login shell when logging in. ② as an interactive shell for non-login shells. ③ runs the script as a non-interactive shell.


Login Shell:

The login shell will look for four different boot files to handle the current command:/etc/profile, $HOME/.bash_profile, $HOME/.bash_login, $HOME/.profile

Where the/etc/profile file is the main default boot file for bash shell on the system, each user on the system will execute this startup file at logon, and three boot files are specific to each user and can customize them according to the needs of each user. When we log in to the Linux operating system, bash executes the commands in the/etc/profile startup file. We also notice that many of the variables in this file are global variables.

The profile file also uses another tricky feature, which is the ability to iterate over a for statement for any file in the/ETC/PROFILE.D directory, which allows the Linux system to provide an application-specific boot file that will be executed by the shell when we log on.

For the three files in the $home directory, they have the same functionality, they provide user-specific startup files that define user-specific environment variables, and most Linux distributions use one of the three, the. bash_profile or. Bash_ Login or. Profile, note that all three of them start with a dot number, indicating that they are hidden files and are not displayed in the normal LS command list.


Interactive shell:

If you launch a bash shell without logging into the system, such as typing bash only at the CLI prompt, we start with an interactive shell that behaves differently from the login shell, but it still provides a CLI prompt for us to enter the command.

If Bash starts as an interactive shell, it does not process the/etc/profile file, instead it checks the. bashrc file in the user's home directory. While the. bashrc file performs two tasks: ① first checks the public BASHRC file in the/etc directory. ② provides a place for users to enter personal aliases and private script functions.

Public/ETC/BASHRC files are run by anyone on the system that initiates an interactive shell session. The default file sets some environment variables, but it does not use the Export command to set them to global properties. Keep in mind that an interactive shell startup file runs every time a new interactive shell is started, so any child shell automatically executes the interactive shell startup file. We will also note that the/ETC/BASHRC file also executes application-specific startup files located in the/ETC/PROFILE.D directory.


non-interactive shell:

It is the shell in which the system starts executing shell scripts. The difference is that you don't have to worry about the CLI prompt, but we still want to run a specific startup command every time we start a script in the system.

The bash shell provides the BASH_ENV environment variable, which examines the name of the pending startup file represented in the environment variable when the shell starts a non-interactive shell process. If the variable has a value, the shell executes the command in the file.


Array of variables:

A very good feature of environment variables is that they can be used as arrays, which are variables that can hold multiple values, and the values in the array can be used separately or as a whole reference.

To set multiple values for an environment variable, we only need to list them in parentheses, with each value separated by a space, such as >xin= (one two three four five) but only the first value of the array is displayed when we use the >echo $xin.

If we are going to refer to a separate array element, we must use a numeric index value that represents the position of the element in the array, the index value in square brackets, for example: Echo ${xin[2]}, and it is important to note that the index value of the environment variable array starts at 0. Of course we can also use the asterisk wildcard character in brackets to see all values, such as Echo ${xin[*]}.

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

Sinsing on the environment variables in Linux

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.