1-3 environmental variables

        Following the above, let's introduce environment variables. In the first two sections, we introduced user-defined variables and location variables. In this section, we introduce environment variables. We only need to understand them. There are not many in our shell scripts. First, let's look at what are environment variables?


  • Linux is a multi tenant operating system, which has a proprietary running environment for different users.
  • The specific environment of different users is a combination of a set of default environment variables.

Classification of environmental variables

  • Environment variables in effect for all users         / etc/profile
  • Environment variables in effect for a specific user         ~/. bashrc or ~ /. bash_profile
  • Temporarily valid environment variables                     The script or command line uses export

         The first / etc/profile is a system level environment variable. The environment variable set in / etc/profile will take effect for all users; There are also environment variables ~ /. bashrc or ~ /. Bash that work for specific users_ Profile, for such environment variables, the ~ /. bashrc file or ~ /. Bash in the specified user's home directory_ Profile file. There may be only one of these two files for different operating systems, or both. Finding any one of them and writing environment variables in it will take effect.


        The first two methods of writing environment variables in the configuration file are permanent, but there is another environment variable, which exists on the command line or in the shell script. We use the export command to import some temporary environment variables. In fact, we also use such temporary environment variables in shell scripts. For example, when we need to start some processes with a specific user, and starting these processes requires some specific environment variables, we can use export to define temporary environment variables for use when starting processes.

        Then, let's take a look at these three environment variables. Our system has provided us with default environment variables. We don't need to set these default environment variables. Let's take a look at the most commonly used. In fact, there are many more.  

        The most common is the PATH environment variable. Its value is a string of paths, which are the paths of all executable files. For example, we execute ls command or find command or nginx command on the command line. Why can these commands be executed without writing absolute paths? Because we already have the PATH of these commands in the environment variable, it knows that the corresponding commands can be found under this PATH, which will be explained in detail later.

        There is also a home environment variable, which is the path to the user's home directory. We know that different users, except the root user, our root user's home directory is the same as the following root. For other ordinary users, we usually create a folder named after the user name under home, so the home directory of this specific user is / home / user name.

        The LOGNAME environment variable indicates the login name of the user. The LOGNAME will be assigned to the logged in user according to which user you use to log in.

        The pwd environment variable represents the current path. For example, if you enter a directory and execute the pwd command, it will directly output the current absolute path.

        The HISTFILE environment variable represents the saved file of the history command. HISTSIZE represents the maximum number of rows saved by the history command. Sometimes we may need to do some auditing to see what commands the user has executed before. We can use the history command on the command line to view the history of our executed commands. How many lines does the history command retain at most? It is set through this HISTSIZE. The default value of HISTSIZE is 1000.

        HOSTNAME represents the host name.

        shell represents the shell currently used by the user.

        PS1 stands for the first level command prompt.

         TMOUT timeout value of user system interaction process

        IFS represents the system input separator

        OFS stands for system output separator

          Let's talk about the export command. We can define variables with the following command:

export VARIABLE1="variable1"


         However, it is usually unnecessary to use export directly in the shell. We usually use export in the shell script to define many variables.


        Next, do some practical demonstrations. We know that the environment variables defined in / etc/profile are valid for all users.

         Let's define Java environment variables. I will download jdk-11.0.12 in advance_ linux-x64_ Put the bin.tar.gz compressed package in the / home/software/1-JDK11 path, and then unzip the compressed package.

         We edit the profile file by executing the following command:

vim /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.

pathmunge () {
    case ":${PATH}:" in
            if [ "$2" = "after" ] ; then

if [ -x /usr/bin/id ]; then
    if [ -z "$EUID" ]; then
        # ksh workaround
        EUID=`/usr/bin/id -u`
        UID=`/usr/bin/id -ru`
    USER="`/usr/bin/id -un`"

# Path manipulation
if [ "$EUID" = "0" ]; then
    pathmunge /usr/sbin
    pathmunge /usr/local/sbin
    pathmunge /usr/local/sbin after
    pathmunge /usr/sbin after

HOSTNAME=`/usr/bin/hostname 2>/dev/null`
if [ "$HISTCONTROL" = "ignorespace" ] ; then
    export HISTCONTROL=ignoreboth
    export HISTCONTROL=ignoredups


# By default, we want umask to get set. This sets it for login shell
# Current threshold for system reserved uid/gids is 200
# You could check uidgid reservation validity in
# /usr/share/doc/setup-*/uidgid file
if [ $UID -gt 199 ] && [ "`/usr/bin/id -gn`" = "`/usr/bin/id -un`" ]; then
    umask 002
    umask 022

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

unset i
unset -f pathmunge

#java Environment Setting
export JAVA_HOME=/home/software/1-JDK11/jdk-11.0.12
export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
export PATH=$PATH:$JAVA_HOME/bin

         We add at the bottom:

#java Environment Setting
export JAVA_HOME=/home/software/1-JDK11/jdk-11.0.12
export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
export PATH=$PATH:$JAVA_HOME/bin

        Note: the definition of environment variables is usually capitalized, which is a default criterion.

         Then use the following command to make the editing changes of the file effective:

source /etc/profile

         Finally, let's verify whether the Java environment variables are configured successfully. Execute under the root user and an ordinary user respectively:


java -version

su lvgp

java -version


          Let's take a look at the environment variable settings for a specific user. Execute under a specific user:

cd ~


ls -al

        You can see. bash_profile file and. bashrc file. We can edit these two files to set environment variables for specific users. Either of the two files can be used. For example, we use. bash_profile.


vim .bash_profile

# .bash_profile

# Get the aliases and functions
if [ -f ~/.bashrc ]; then
        . ~/.bashrc

# User specific environment and startup programs


export PATH

TEST_VARIABLE="test variable"

        Let's add a test at the bottom_ VARIABLE="test variable"

        We use source   . bash_ Use the profile command to reread the configuration file. Then execute:


          However, this is only the private environment variable of a specific user, which does not take effect under other users. For example, we switch to root.



        You can see test_ The variable variable is a null value.

        Other environment variables are relatively simple, so we won't demonstrate them one by one. If you are interested, you can learn by yourself. If you have any supplement, please leave a message. I will improve the article after verification. thank you!


Tags: shell

Posted on Sat, 30 Oct 2021 01:00:18 -0400 by unstable_geek