March 19, 2019
3min Read
Edward S.
In this tutorial, we’ll explain in-depth what Linux environment variables are, and teach you basic modification and read commands on your VPS to get you started! Let’s jump in!
In computer science, a variable is a location for storing a value that can be changed depending on conditions or information passed to the program. Variables play an important role in programming, they enable developers to write flexible programs related to the operating system they work on!
Environment variables are dynamic values that affect the programs or processes running on a server. They exist on every operating system, and their type may vary. Environment variables can be created, edited, saved and deleted.
Linux environment variables are placeholders for information stored within the system that passes data to programs launched in shells or sub-shells.
Let’s look at some common Linux environment variable commands that you might want to know. Remember, before modifying any variables, you need to access your VPS using SSH.
You can see the entire list of environment variables on your Linux distribution by using the printenv command. The simple use of it on Ubuntu will provide a large output displaying the variables.
You can get a more manageable output by adding piping in a modifier:
printenv | less
Every line contains the name of the Linux environment variable followed by = and the value. For instance:
HOME=/home/edward
HOME is a Linux environment variable that has the value set as the /home/edward directory.
Environment variables are typically upper case, though you can create lower case environment variables as well. The output of printenv displays all environment variables in uppercase.
An important thing to note is that Linux environment variables are case sensitive. If you want to view the value of a specific environment variable, you can do so by passing the name of that variable as an argument to the printenv command. The entire string would look like this in the command line:
printenv HOME
Output:
/home/edward
Another way to display the value of an environment variable is by using the echo command like this:
echo $USER
Output:
Edward
The basic syntax of this command would look like this:
export VAR="value"
Let’s break it down:
In a real world scenario the command could look like this:
export edward="hostinger"
Let’s see how we could change the value of the TZ – timezone – variable:
First, let’s view the time:
date
The command will output the current time.
Then we can use the export command to alter the timezone:
export TZ=”US/Pacific”
Now that the variable’s value was changed, we can check the time again by using the date command, that would output a different time appropriate to the changes made to the Linux environment variable.
We’ll be using the unset command. Let’s see how the command’s syntax looks, and break it down:
unset VAR
The parts of the command are:
Simple, right? We’ll unset the timezone variable as a test:
unset TZ
This will take time zone to its default value, which we can check by using the date command once more.
Setting and unsetting a Linux environmental variable from the command line effects only your current running sessions. If you want to make your settings persist between logins you have to define the environment variables in your personal initialization file – i.e. .bash_profile.
In computer programming, a global variable is one that can be used anywhere in the program. While a local variable is one that is defined in a function and can only be used in that function. Here’s an example – Global_var and local_var are the global and local variables:
Var Global_val=50; Function Fun() { var local_var =20; }
Linux environment variables can be global or local. The global environment variables are visible from a shell session and any child processes that the shell spawns. While local variables can be available in the shell only in which they are created.
System environment variables use all uppercase letters to distinguish them from normal user environment variables.
In the following example, local_var is only visible in the current shell:
local_var=edward echo $local_var edward
We can create a global environment variable using the export command:
export Global_var=Hello bash echo $Global_var
Finally the output will be:
Hello
Congratulations, now you know all the Linux environment variable basics! Remember to always be careful, research what you’re modifying and keep advancing your skills! We hope this tutorial helped you do just that!
Leave a reply