Environment variables are one of those Linux things that, once they click, you’ll be using them everywhere.
In this tutorial we’ll dabble a bit with environment variables, and see how they can be used in shell scripts. Everything we do in this tutorial will be done in the terminal, so you’ll get some good practice with the Linux command line too.
But first, environment variables: what are they?
What are environment variables?
Environment variables (or “env vars” if you want to save some breath) are a way of storing configuration information in Linux. They’re a good way to store configuration, or temporary data that you want to use in your programs.
Linux usually has a bunch of environment variables that are already defined. They’re like built-in properties. They tell you information about your system, such as the current user, the current working directory, and the current shell. They’re called things like
You can also add your own custom environment variables to store information about your own programs.
What you’ll need for the tutorial
A Linux terminal, or a virtual machine running Linux. Any distribution of Linux should be fine.
You should have experience editing files using a text editor such as
Looking at environment variables
First let’s explore the environment variables that are already in your system, probably without you even realising it.
Print your system’s environment variables. Run the following command to print your system’s environment variables:
Now you should see a list of environment variables, like
SHELL. For example, the output on my system looks like this:
SHELL=/bin/bash SESSION_MANAGER=local/unix:@/tmp/.ICE-unix/5046,unix/unix:/tmp/.ICE-unix/5046 COLORTERM=truecolor HISTCONTROL=ignoredups XDG_MENU_PREFIX=gnome-
You can also use the
printenvcommand to print the value of a specific environment variable. For example, try typing
printenv HOMEand seeing what you get!
Search for a string in an environment variable. Next, run the following command to search for a string in an environment variable name or value:
printenv | grep SHELL
This command uses the
grepcommand to search for the string
SHELLin the output of
Grep is an absolutely essential command for searching for things. You will probably use it every day.
We can also search for strings in the values of our environment variables. So let’s search for the string
printenv | grep bash
If you’re using bash as your Linux shell, you should see a line like this:
Using environment variables in a command
Now that we’ve seen how to print and search for environment variables, how can we use them?
One of the most common times you might use environment variables is when you’re running a command.
For example, you might want to run a command on a specific file, but you don’t want to type the full path to the file.
Or, you might want to give a sensitive password to a command, without having to type it in, or show it on-screen.
Let’s use an environment variable in a command.
Pass an environment variable to the
echocommand. The echo command is one of the simplest Linux commands there is! It just prints whatever you pass to it.
Run the following command to print the value of the
HOMEenvironment variable. We’re using
HOMEbecause it’s a variable that’s set on all Linux systems:
Note that we use the special
$HOMEsyntax to access the value of the
You should see output like this:
(Note that your username will be different!)
Use an environment variable in another command. Now let’s use the
HOMEenvironment variable in another command. Run the following command:
This command uses the
cdcommand to change the current working directory to the value of the
You can always check the current working directory using the
Now if you run the
pwdcommand, you should see that you’ve been taken to your home directory!
How did that work?
So what just happened? When you type in a command with an environment variable, a substitution happens.
When you type the command
echo $HOME, the shell replaces the variable reference
$HOME with the value of the
HOME environment variable.
This means that Bash (or whichever shell you use) is responsible for the substitution. It’s not the
echo command that does it.
So the first command that was actually run, was this:
Using environment variables in a shell script
Now let’s get a bit more creative, and use environment variables in a shell script.
Create the script
Create a shell script. Create a file called
hello.sh. Remember you can create a file using the
touchcommand, or using your text editor like
pico. Then add the following code to it:
#!/bin/bash echo "Hello, world!"
This is a simple shell script that prints “Hello, world!” to the terminal.
#!/bin/bashis called a shebang. It tells the operating system that this file is a shell script, and that it should be run using the Bash shell.
Make the script executable. In Linux, scripts aren’t executable by default, so we need to run the following command to make it executable:
chmod +x hello.sh
We use the
chmodcommand to change the permissions of a file. The
+xoption means “add execute permissions”.
You can always check the permissions of a file using the
ls -lcommand. For example,
ls -l hello.sh.
Run the script. Run the following command to run the script:
Add environment variables to the script
Now that we have our test script, let’s add some environment variables.
Reference an environment variable in the script. Adding variables in shell scripts makes them more dynamic.
Let’s change the greeting to say hello to the current user. Fortunately in Linux there is usually an environment variable called
USERNAMEthat stores the current user’s username.
So edit the file so it looks like this:
#!/bin/bash echo "Hello, $USERNAME!"
$USERNAMEsyntax is used to access the value of the
Run the script. Once you’ve made the changes above, run the modified script.
You should see this output:
(Again, or whatever your username is!)
Define our own environment variables
Finally, to finish off, let’s define our own environment variables.
Define an environment variable. Run the following command to define an environment variable called
exportcommand is used to define an environment variable. The syntax is
You can also define an environment variable without the
exportcommand, but it will only be available in the current shell session. Using
exportmakes it available to all child processes, including the programs you run.
Edit the script to print the environment variable. Now let’s edit the script to use the
SECRET_CODEenvironment variable. Edit the file so it looks like this:
#!/bin/bash echo "Hello, $USERNAME!" echo "Your secret code is $SECRET_CODE"
Now when we run the script, it will print the value of the
Run the script and let’s see it in action.
You know what to do:
And you should see:
Hello, tdonohue! Your secret code is 1234
And that’s the end of the tutorial!
You did it. You’ve completed the tutorial!
In this tutorial, you learned how to:
- Print your system’s environment variables
- Search for a string in an environment variable
- Pass an environment variable to the
- Use environment variables in a shell script
See you in the next tutorial!