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 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:
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:
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 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:
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:
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.
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 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:
- 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’.
- 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.
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 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
- Open a Terminal
- Type the command: A=10
- Type the command: echo $A
- Type the command: bash
- Type the command: echo $A
- Type the command: exit
- Type the command: echo $A
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:
- Open a Terminal
- Type the command: A=10
- Type the command: echo $A
- Type the command: bash
- Type the command: echo $A
- Type the command: exit
- Type the command: echo $A
- Type the command: export A
- Type the command: bash
- Type the command: echo $A
- Type the command: exit
- Type the command: echo $A
- Open a second Terminal
- Type the command: echo $A
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: