Environment variables in Linux

Home > Search
  by

The echo command followed by the $ character and then a string of data is what is used to display the contents of a variable. In this example, the tmpDir variable either does not exist or does not contain any data.

~]# echo $tmpDir

 


Temporary

The most common use of temporary variables is inside of a bash shell script. For example, let's say a bash script writes files to a temporary directory, such as /tmp/myFiles. Instead of listing /tmp/myFiles over and over again in the script, a variable can be used. In this example, the variable named tmpDir will be /tmp/myFiles.

tmpDir="/tmp/myFiles"

 

There are numerous ways to use the tmpDir variable. You can echo the variable.

~]# echo $tmpDir
/tmp/myFiles

 

You can list the variable.

~]# ls $tmpDir
file1.txt file2.txt

 

You can cat the variable.

~]# cat $tmpDir/file1.txt
Hello World

 

If the variable is created inside of a function, you may need to export the variable to be able to use it in other functions.

~]# export tmpDir

 

If you want to be certain that the variable will not be used when done with your script, unset the variable.

~]# unset tmpDir

 


Permanent

To ensure the variable is permanent, add the variable to one of the bash files:

  • ~./bash_profile or ~/.profile (user specific)
  • /etc/profile (all users)

 

Add HW to ~./bash_profile or ~/.profile.

export HW="Hello World"

 

Close the current shell, open a new Terminal, echo $HW, and there is no output. This is because you need to reload the current environment.

[user1@server1 ~]# echo $HW

 

Reload the environment by switching to the root account and then back to user1 account. Include - in the SU command to ensure the enviroment is reloaded.

[user1@server1 ~]# su - root
[root@server1 ~]# su - user1

 

Echo $HW, and Hello World will appear. 

[user1@server1 ~]# echo $HW
Hello World

 

Likewise, HW will be listed in the ENV and SET commands. Adding a variable to ~/.bash_profile or ~/.profile makes the variable permanent.

[user1@server1 ~]# env | grep HW
HW="Hello World"

[user1@server1 ~]# set | grep HW
HW="Hello World"

 

Open two Terminals, one as user1 and another as root, and try to echo $HW. In user1 shell, Hello World appears. In root shell, there is no output. This is because the ~ in ~/.bash_profile or ~/.profile represents the current users shell.

[user1@server1 ~]# echo $HW
Hello World

[root@server1 ~]# echo $HW

 

If you are in user1 shell, home is /home/user1:

[user1@server1 ~]# echo ~
/home/user1

[user1@server1 ~]# echo $home
/home/user1

 

If you are in root shell, home is /root.

[root@server1 ~]# echo ~
/root

[root@server1 ~]# echo $home
/root

 

Because export HW="Hello World" was added to /home/user1/.profile, this environment variable will only work when using the user1 shell. We could of course add export HW="Hello World" to root ~/.bash_profile or ~/.profile file. However, this is not a great solution for systems with many users, or when new users accounts will be added or removed. To add the HW variable to be available in every users shell, add export HW="Hello World" to the /etc/profile file.

The /etc/profile file has permisson 644 (-rw-r--r--), so only root can edit this file. You will either need to switch to the root account, or use sudo to write changes to this file.

Switch between root and user1 account to reset the environment variables.

[user1@server1 ~]# su - root
[root@server1 ~]# su - user1

 

Both user1 and root will be able to echo HW.

[user1@server1 ~]# echo $HW
Hello World

[root@server1 ~]# echo $HW
Hello World

 

And both ENV and SET will have the variable. All users can use the HW variable, and the variable is permanent.

[user1@server1 ~]# env | grep HW
HW="Hello World"

[user1@server1 ~]# set | grep HW
HW="Hello World"

 

Because a variable does not remain in tact after a shell is closed, you might be wondering why a variable would ever be created but not listed in the /etc/profile file. It is very common to not put a variable in /etc/profile when using a .bash or .sh script. For example, let's say you create a shell script with a variable such as ProductName="Our current awesome product name." This is a smart approach, because if the product name is changed, all you have to do is to revised the ProductName variable in the shell script, instead of replacing every instance of the product name in the shell script. Also, this variable does not need to be placed in the /etc/profile file, as it is only needed when executing the .bash or .sh script that contain the ProductName variable.


Set can also be used to set the options of a file. Let's say you have a very important file that you must guarantee is never overwritten. The noclobber option can be set on the file.

[user1@server1 ~]# set -o noclobber file1

 

We can verify the noclobber option is set on the file.

[user1@server1 ~]# set | grep file1
SHELLOPTS=braceexpand:emacs:hashall:histexpand:history:interactive-comments:monitor:noclobber

 

If we attempt to overwrite the file, we are prevented from doing so.

[user1@server1 ~]# echo "Hello World" > file1
bash : file1 : cannot overwrite existing file

 



Add a Comment




We will never share your name or email with anyone. Enter your email if you would like to be notified when we respond to your comment.




Please enter in the box below so that we can be sure you are a human.




Comments