Environment variables in the bash shell

Source: Internet
Author: User
Tags aliases


Environment VariablesThe --bash shell uses environment variables to store system-related data and allows data to be stored in memory.







Environment variables are divided into: Global environment variables



Local environment variables





Directory


    • Global environment variables

    • Local Environment variables

    • Setting Global environment variables

    • Delete environment variables

    • Path Global environment variable

    • Set up related files for system environment variables (login, non-login, interactive, non-interactive shell)




First, Global environment variables



Visible in both the current shell and the child shell



You can use the PRINTENV command to view Global environment variables, uppercase indicates system environment variables, lowercase means environment variables for ordinary users



This is a standard convention for bash shells, not required, so we use lowercase when setting new environment variables to differentiate between individual and system environment variables.




[[email protected] ~]# printenv
TERM=linux
SHELL=/bin/bash
HISTSIZE=1000
SSH_CLIENT=172.18.251.124 8132 22
QTDIR=/usr/lib64/qt-3.3
QTINC=/usr/lib64/qt-3.3/include
SSH_TTY=/dev/pts/4
Name = Hello ා self defined environment variable
USER=root
LS_COLORS=...
MAIL=/var/spool/mail/root
PATH=/usr/lib64/qt-3.3/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
PWD=/root
LANG=en_US.UTF-8
PS1=[\[\e[33m\]\[email protected]\[\e[34m\]\h \[\e[m\]\W]\$ \[\e[m\]
SSH_ASKPASS=/usr/libexec/openssh/gnome-ssh-askpass
HISTCONTROL=ignoredups
PS2=\[\e[34m\]> \[\e[m\]
SHLVL=1
HOME=/root
LOGNAME=root
QTLIB=/usr/lib64/qt-3.3/lib
CVS_RSH=ssh
SSH_CONNECTION=172.18.251.124 8132 172.18.250.183 22
LESSOPEN=||/usr/bin/lesspipe.sh %s
DISPLAY=localhost:12.0
G_BROKEN_FILENAMES=1
_=/usr/bin/printenv


Most variables are set when you log in to the main shell.



Second, local environment variables



Visible only in the current shell



Can be viewed through the SET command, but the set command looks at all environment variables (global and local)






Note that when setting the environment variable,you cannot add a space between [variable = value] , otherwise the shell will execute it as a separate command





third, set the Global environment variable    



To change a local environment variable to a global environment variable by using the Export command






IV. Deleting environment variables



Use the unset command to delete an environment variable in the format



unset variable Name



However, for the deletion of global environment variables, we should pay attention to:



If you delete a global environment variable under a child shell, the delete operation is only valid for the child shell, and if you go back to the parent shell, the global variable can also reference



v. PATH Global environment Variables



To modify the PATH environment variable:



Path= $PATH: New Add-on directory



Tips:



We can set PATH to path= $PATH:. (a single point represents the current working directory)







Vi. setting the relevant files for the system environment variables



The system environment variable is the execution of the associated file definition during shell startup. These files are called Shell startup files. But when we set the system environment variables, we want to differentiate between the login shell, the non-logon shell, the interactive shell, the non-interactive shell, (Login/non-login and interaction/ Non-interaction is not the same as dividing the standard, only the shell of the different patterns can be correctly modified to correct the corresponding shell boot files so that the system environment variables can be set correctly.



Just as recently, the Linux system startup process is also being contacted, which involves logging into a shell.





6.1 Log-in shell



The login shell is the shell that the user needs to enter the user name and password, which in turn executes the following files in the shell startup process.



/etc/profile # Log in to bash Shell's default master boot file. Any user who logs on to the shell executes this startup file. Modification is not recommended



~/.bash_profile



~/.bash_login



~/.profile # Appeal These three $home startup files are environment variables that define the corresponding user. Different files for Linux distributions



The commands and scripts in/etc/profile are not our concern now, we mainly look at the export line, so we can know that the file is set the system Global environment variable



/etc/profile Another important feature is the ability to repeat the/etc/profile.d/ Files in the directory (mostly files ending with. Sh and. csh), which are probably startup files for a particular application, can set environment variables for the relevant application, such as /etc/profile.d/lang.*sh , which is used to set the lang environment variable.    


[[email protected] ~]# cat /etc/profile
# /etc/profile

# System wide environment and startup programs, for login setup
# Functions and aliases go in /etc/bashrc

# It's NOT a good idea to change this file unless you know what you
# are doing. It's much better to create a custom.sh shell script in
# /etc/profile.d/ to make custom changes to your environment, as this
# will prevent the need for merging in future updates.
...

export PATH USER LOGNAME MAIL HOSTNAME HISTSIZE HISTCONTROL

...

for i in /etc/profile.d/*.sh ; do
    if [ -r "$i" ]; then
        if [ "${-#*i}" != "$-" ]; then
            . "$i"
        else
            . "$i" >/dev/null 2>&1
        fi
    fi
done

unset i
unset -f pathmunge
[[email protected] ~]#


$HOME startup files, my system uses the ~/.bash_profile, these files all start with., which means that both are hidden files and are specific to the user, so the user can modify the file.



Let's look at the contents of the ~/.bash_profile file to define the line of path. $HOME file defines the path= $PATH for a specific user: $HOME/bin, which means that we can place the executable in the $HOME/bin directory.




[[email protected] profile.d]# cat ~/.bash_profile 
# .bash_profile
# Get the aliases and functions
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
# User specific environment and startup programs
PATH=$PATH:$HOME/bin
export PATH




6.2 Non-sign-on shell



The login shell is a shell that needs to enter a username and password, not a login shell, such as typing bash directly at the command line, clicking Open in Terminal on the command line terminal in the graphical interface, and so on, are non-login shells.



In addition, the Exit command can exit the login shell and thenon-login shell,logout only exit the login shellfor the difference between exit and logout from the shell.






We can see if it is a login shell or a non-login shell through the value of the $ variable , and the login shell will appear before the '-' non-login shell is not





During the boot process of the non-logon shell, the non-login shell only needs to execute the following files because it does not require a duplicate login shell.



$HOME/.BASHRC # The following content explains




[[email protected] ~]# cat ~/.bashrc 
# .bashrc
# User specific aliases and functions
alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'
alias cdnet='cd /etc/sysconfig/network-scripts/'
alias ping='ping -c 4'
# Source global definitions
if [ -f /etc/bashrc ]; then
. /etc/bashrc
fi


The $HOME/.BASHRC can define user-defined aliases and functions, as well as references to variables under public/ETC/BASHRC, let's take a look at the contents of the/ETC/BASHRC file


[[email protected] ~]# cat /etc/bashrc 
# /etc/bashrc

# System wide functions and aliases
# Environment stuff goes in /etc/profile

# It's NOT a good idea to change this file unless you know what you
# are doing. It's much better to create a custom.sh shell script in
# /etc/profile.d/ to make custom changes to your environment, as this
# will prevent the need for merging in future updates.

# are we an interactive shell?
...

    # Only display echos from profile.d scripts if we are no login shell
    # and interactive - otherwise just process them to set envvars
    for i in /etc/profile.d/*.sh; do
        if [ -r "$i" ]; then
            if [ "$PS1" ]; then
                . "$i"
            else
                . "$i" >/dev/null 2>&1
            fi
        fi
    done

    unset i
    unset pathmunge
fi
# vim:ts=4:sw=4


In addition, the file executes/etc/profile.d/*.sh to set the environment variables for a particular application.



In fact, the login shell will also execute the $HOME/.BASHRC, you can go back to the ~/.bash_profile code section above, we will find that the file will call the $HOME/.BASHRC file. This can deepen the essential difference between the login shell and the non-login shell.



6.3 Interactive Shell



We log in to Linux through the terminal, enter the command, the shell executes the command and returns the result in real time, exits. This mode is interactive shell.



Under the interactive shell, bash does not execute the/etc/profile file, instead of the $HOME/.BASHRC file, which executes the same boot file as the non-login shell .



This file defines the environment variables for the new interactive shell, which is best not to define global environment variables (export), and the file also executes/etc/profile.d/*.sh to set environment variables for a particular application. Any operation that opens the interactive child shell (bash, su-user) will read the $HOME/.BASHRC.



6.4 Non-interactive shell



In contrast to the interactive shell, the shell does not interact with the terminal, such as a shell script to read commands in the script, without having to interact with the terminal (unless the user is required to enter parameters), and the shell exits when the file ends.



A Global environment variable bash_env related to a non-interactive Shell's related startup file and system settings. The variable is not defined by default. We need to set this variable manually and execute the file that the variable points to when executing the shell script.









We can use the variable value of $-to see whether the current shell is interactive or non-interactive, such as:




vim tmp.sh
#!/bin/bash
echo $-




To distinguish between interactive and non-interactive is to see if there is ' I ' (interactive), you can see the script is non-interactive, we usually use the terminal for interactive.



6.5 Summary



The login shell, which includes the files to be called in the boot file and the file Code section to be executed sequentially, summarizes them as follows:





Non-sign-on shell





Interactive shell



Executing the startup file procedure is similar to a non-logon shell



Non-interactive shell



Executes the file that the BASH_ENV Global environment variable points to



Knowing the differences between these startup files, we can modify the startup file to make the custom Global environment variable, alias, etc. permanent, for example, we can put all the custom global environment variables in a file ending in. sh, and then put the file in/etc/profile.d/ Directory or place a custom variable into the/ETC/BASHRC file, which will take effect for all users. For some aliases for individual users, you can write them to the ~/.BASHRC file, which is only valid for a single user.






Environment variables in the bash shell


Related Article

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.