LFCS – System Variables

Jarret B

Well-Known Member
Staff member
Joined
May 22, 2017
Messages
339
Reaction score
369
Credits
11,689
The Linux Operating System (OS) uses variables to represent other settings. If a variable, such as the logged in user’s name, is needed in different applications then it is best to set the value as a variable.

There is a need to create a variable, read its value and remove the variable if it is not needed.

There are two main types of system variables: Environment Variables and Shell Variables.

Environment Variables

Environment variables are basically considered Global Variables only within the shell and its child processes. For example, the Linux System will set the primary language in the variable ‘LANGUAGE’.

To see the contents of the variable you can use the command:

echo $<variable_name>

To see what LANGUAGE the current system is using you can use either of the following commands in a Terminal:

echo $LANGUAGE
echo $LANG


NOTE: Both commands will work on Ubuntu, but only the LANG variable shows a result on CentOS.

My system has the result of: ‘en_US’.

To see the list of existing environment variables on a Linux System use the command ‘env’ or ‘printenv’ in a Terminal.

The difference between the two is that ‘printenv’ will allow you to see a specific variable. The ‘env’ command will let you modify a variable.

The ‘printenv’ command will return a result similar to Figure 1.

Figure 01.jpg

FIGURE 1

NOTE: Whether using CentOS or Ubuntu the variables should be very similar. There may be a few differences, but the main variables are standard for all Linux Systems.

If I wanted to find an Environment Variable and I could not remember the full name of it, but I knew it started with an ‘L’, I could do the following:
  1. In a Terminal type ‘echo $’ and the first letter I remember (or more letters). Remember that the variable names are case-sensitive. If I think it is an ‘L’ I would type ‘echo $L’.
  2. Press the TAB key twice. You should be given a list of all of the variables which start with the letter or letters you specified. If no list is shown then it is because none exist.
From the example of ‘echo $L’ the results could be similar to Figure 2.

Figure 02.jpg

FIGURE 2

With the command ‘printenv’ you can specify a variable to list. For example, to see the currently logged on user the variable ‘USER’ would be used. The command would be ‘printenv USER’ in a Terminal.

The command ‘env’ allows you to modify a variable. Such as to modify the variable you would also specify the new value. For example, if a variable existed called ‘A’ and it had a value of 10 you could change it. The command to change the value to 20 would be ‘env A=20’.

Some of the most common Environment Variables are:
  • SHELL – interpreting shell (usually BASH)
  • USER – currently logged in user
  • PWD – present working directory
  • OLDPWD – previous present working directory
  • MAIL – path to current user’s mailbox
  • PATH – folders to check for applications, folders are checked in order
  • LANG – default language
  • HOME – current user’s Home folder
  • _: - most recently executed command

Let’s run a little test. Create a script file named ‘script.sh’ in your HOME folder with the following contents:

#!/bin/bash
echo $USER


In a Terminal make sure you are in the HOME folder with the command ‘cd ~’. Then run the command ‘chmod +x script.sh’ to make the script executable. Run the script with the command ‘./script.sh’. You should see the username of the currently logged in person. Now, try the command ‘sudo ./script.sh’. The result should now be ‘root’ since you used the sudo command to execute the command as root.

NOTE: Variables set by the system cannot be changed with ‘env’. Some Shell Variables can be changed. Some variables can only be changed by the system, such as USER.

SHELL VARIABLES

The Shell you use, for most it will be BASH, is where the SHELL Variables are set. To see all the variables on a system use the command ‘set’ from a Terminal. The ‘set’ command will display Environment Variables, Shell Variables and Shell Functions.

An example of the ‘set’ command is shown in Figure 3.

Figure 03.jpg

FIGURE 3

Also listed with the variables are Shell Functions which appear at the end of the listing.

Some of the more common Shell Variables are:
  • BASHOPTS – options used when BASH is executed
  • BASH_VERSION – the version of BASH
  • HISTFILES – number of lines of stored history entries
  • HISTSIZE – number of lines of command history to store
  • HOSTNAME – name of the Host System
Shell Variables are stored and used only within the current shell. Let’s perform an example with the following instructions (assuming you use BASH as your shell):
  1. Open a Terminal
  2. Type the command: A=10
  3. Type the command: echo $A
  4. Type the command: bash
  5. Type the command: echo $A
  6. Type the command: exit
  7. Type the command: echo $A
Step 4 opens a new child shell within the existing one. The variable ‘A’ is not inherited since it is a Shell Variable as shown in Step 5. In Step 6 the child process is closed placing you back in the original shell. Step 7 shows you are in the original shell since the variable ‘A’ exists.

The only way for the variable ‘A’ to work in both shells is to change it to an Environment Variable.

Changing Variable Types

What if you want a shell variable to be available in all shells? You need to change the type of variable from Shell to Environment.

To change the variable type you use the command ‘export’.

Let’s test the export ability with another example. Close all Terminals before performing this example so no Shell Variables are still set. Once the Terminals are closed, perform the following:
  1. Open a Terminal
  2. Type the command: A=10
  3. Type the command: echo $A
  4. Type the command: bash
  5. Type the command: echo $A
  6. Type the command: exit
  7. Type the command: echo $A
  8. Type the command: export A
  9. Type the command: bash
  10. Type the command: echo $A
  11. Type the command: exit
  12. Type the command: echo $A
  13. Open a second Terminal
  14. Type the command: echo $A
The first 7 Steps work the same as before in the Shell example. In Step 8 the variable ‘A’ is exported as an Environment Variable. Step 10 proves that the Environment Variable was inherited by the child process when the result was ‘10’. Step 12 showed that the variable still existed when the child process closed. Step 14 shows that the variable is not available in a process which is not a child, but would be considered a sibling process since both Terminals were started by the Host System. Even if we started a new Terminal from the Terminal started in Step 1, the variables are not inherited. For example, from the Terminal started in Step 1, select ‘Help’ and then ‘About’ from the menu. A window should appear which tells you the name of the Terminal program and its version. Remember the name of the Terminal. Close the About box and at the Terminal prompt type in the Terminal name and press ENTER. A new Terminal should open. Technically this new Terminal is not a child process. You can test this by typing the command ‘echo $A’. The result should be blank showing there is nothing inherited so the Terminal is not a child process.

NOTE: A quick step to creating an Environment Variable is to simply type ‘export <variable>=<value>’ with your variable name and its value. You do not need to create a Shell Variable first and then export it. It can be done in one step. For example, to create an Environment Variable named ‘B’ with a value of ‘Hi there.’ the command would be: ‘export B=“Hi there.”’. Since the value contains a space the string must be enclosed in double-quotes.

What if you wanted to change an Environment Variable back to a Shell Variable? The command to use is ‘export -n <variable>’. If the variable B were still an Environmental Variable, the command to make it back to a Shell Variable is ‘export -n B’.

To completely remove a set variable use the command ‘unset <variable>’. So, to remove the variable B the command would be ‘unset B’.

NOTE: To remove the variables you have created can be accomplished by closing the Terminal in which you created the variables.

Variables can be created specifically for a single user or all users logging onto a system.

Setting User and System Variables

To set variables for a specific user, enter the variable and value into the file ‘~/.bashrc’. Go to the end of the file and add the variable and value as as export command. For example, to set the Variable ‘A’ to a value of ‘10’ you would add the line ‘export A=10’ to the end of the file and save it.

NOTE: For the values to take effect immediately the user needs to run the command ‘source ~/.bashrc’.

To set variables for all users to log into a system the same method is used except the file to use is /etc/bash.bashrc, /etc/profile or /etc/environment.

I hope this helps you understand the different variables and how to use them. Practice on these before going onto another LFCS article.
 
Last edited:

Members online


Top