Modifying a shell environment

When a new shell is launched, it has the initial environment set that will be used by any application or command that gets executed in a given shell. We now know that the env or setenv shell builtin command can be used to view which environment variables are set for this shell. The shell also provides the capability to modify the current environment. We can also modify the current bash environment by creating, modifying, or deleting environment variables.

Creating environment variables

To create a new environment variable in a shell, the export shell builtin command is used.

For example, we will create a new environment variable ENV_VAR1:

$ env | grep ENV_VAR1  # Verifying that ENV_VAR1 doesn't exist
$ export ENV_VAR1='New environment variable'

A new environment variable with the name ENV_VAR1 is created. To view a new environment variable, we can call the printenv or env command:

$ env | grep ENV_VAR1
ENV_VAR1=New environment variable
$ printenv ENV_VAR1    # Viewing value of ENV_VAR1 environment variable
New environment variable

We can also use the echo command to print the value of an environment variable:

$ echo $ENV_VAR1  # Printing value of ENV_VAR1 environment variable
New environment variable

A local shell variable can also be exported further as an environment variable. As an example, we will create the ENV_VAR2 and LOCAL_VAR1 variables:

$ ENV_VAR2='Another environment variable'
$ LOCAL_VAR1='Local variable'
$ env | grep ENV_VAR2  # Verifying if ENV_VAR2 is an environment variable

No environment variable is found with the name ENV_VAR2. This is because while creating ENV_VAR2, it wasn't exported. Therefore, it will be created as a local variable of a shell:

$ set | grep ENV_VAR2
ENV_VAR2='Another environment variable'
$ set | grep  LOCAL_VAR1
LOCAL_VAR1='Local variable'

Now, to make the ENV_VAR2 shell variable as an environment variable, we can use the export command:

$ export ENV_VAR2    # Becomes environment variable
$ printenv ENV_VAR2    # Checking of  ENV_VAR2 is an environment variable
Another environment variable
$ printenv LOCAL_VAR1

The variable LOCAL_VAR1 is not an environment variable.

One of the important features of environment variables is that it is available to all of its child shells. We can see this in the following example:

$ bash  # creating a new bash shell
$ env | grep ENV_VAR2  # Checking if  ENV_VAR2 is available in child shell
ENV_VAR2=Another environment variable
$ env | grep ENV_VAR1
ENV_VAR1=New environment variable
$ env | grep LOCAL_VAR1

We can see that the environment variables from a parent shell got inherited by a child shell—for example, ENV_VAR1, ENV_VAR2—while the local variable, such as LOCAL_VAR1, remains available only to a shell in which the variable was created.

Modifying environment variables

Shell provides flexibility for modifying any existing environment variable. For example, consider the HOME environment variable. By default, the HOME environment variable contains the path of the current logged in user's home directory:

$ printenv HOME
/home/foo
$ pwd    # Checking current working directory
/tmp
$ cd $HOME    # Should change directory to /home/foo
$ pwd    # Check now current working directory
/home/foo

Now, we will modify the HOME environment variable value to /tmp:

$ HOME=/tmp    # Modifying HOME environment variable
$ printenv HOME    # Checking value of HOME environment variable
/tmp
$ cd $HOME    # Changing directory to what $HOME contains
$ pwd    # Checking current working directory
/tmp

We can also append a value to an environment variable. To do this, make sure the new value is separated with a colon (:). For example, consider the PATH environment variable:

$ printenv PATH
usr/lib64/ccache:/bin:/usr/bin:/usr/local/bin:/usr/local/sbin:/usr/sbin:/home/foo/.local/bin:/home/foo/bin

Now, we want to add a new path to the PATH variable—for example, /home/foo/projects/bin—so that, while looking for a program or command, the shell can search the specified path too. To append a path to the PATH environment variable, use a colon (:) followed with a new path name:

$ PATH=$PATH:/home/foo/projects/bin  # Appends new path
$ printenv PATH
usr/lib64/ccache:/bin:/usr/bin:/usr/local/bin:/usr/local/sbin:/usr/sbin:/home/foo/.local/bin:/home/foo/bin:/home/foo/projects/bin

We can see that the new path has been appended to the existing values of the PATH variable.

We can also append multiple values to an environment variable; for that, each value should be separated by a colon (:).

For example, we will add two more application paths to the PATH variable:

$ PATH=$PATH:/home/foo/project1/bin:PATH:/home/foo/project2/bin
$ printenv PATH
usr/lib64/ccache:/bin:/usr/bin:/usr/local/bin:/usr/local/sbin:/usr/sbin:/home/foo/.local/bin:/home/foo/bin:/home/foo/projects/bin:/home/foo/project1/bin:PATH:/home/foo/project2/bin

The two new paths, /home/foo/project1/bin and /home/foo/project2/bin, have been added to the PATH variable.

Deleting environment variables

We can delete or reset a value of an environment variable using the unset shell builtin command.

For example, we will create an environment variable called ENV1:

$ export ENV1='My environment variable'
$ env | grep ENV1  # Checking if ENV1 environment variable exist
ENV1=My environment variable
$ unset ENV1    # Deleting ENV1 environment variable
$ env | grep ENV1

The environment variable ENV1 gets deleted by the unset command. Now, to reset an environment variable, assign it a blank value:

$ export ENV2='Another environment variable'
$ env | grep ENV2
ENV2=Another environment variable
$ ENV2=''	# Reset ENV2 to blank
$ env | grep ENV2
ENV2=
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.117.145.11