Using environment variables in shell scripts: Tutorial

Learn how to print, find, declare and use environment variables in shell scripts

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 USERNAME, PWD and SHELL.

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 nano or vim.


Looking at environment variables

First let’s explore the environment variables that are already in your system, probably without you even realising it.

  1. 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 HOME, PATH, and SHELL. For example, the output on my system looks like this:


    You can also use the printenv command to print the value of a specific environment variable. For example, try typing printenv HOME and seeing what you get!

  2. 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 grep command to search for the string SHELL in the output of printenv.

    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 bash:

    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.

  1. Pass an environment variable to the echo command. 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 HOME environment variable. We’re using HOME because it’s a variable that’s set on all Linux systems:

    echo $HOME

    Note that we use the special $HOME syntax to access the value of the HOME variable.

    You should see output like this:


    (Note that your username will be different!)

  2. Use an environment variable in another command. Now let’s use the HOME environment variable in another command. Run the following command:

    cd $HOME

    This command uses the cd command to change the current working directory to the value of the HOME environment variable.

    You can always check the current working directory using the pwd command.

    Now if you run the pwd command, 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: echo /home/username.

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

  1. Create a shell script. Create a file called Remember you can create a file using the touch command, or using your text editor like vim, nano or pico. Then add the following code to it:

     echo "Hello, world!"

    This is a simple shell script that prints “Hello, world!” to the terminal.

    The line #!/bin/bash is 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.

  2. 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

    We use the chmod command to change the permissions of a file. The +x option means “add execute permissions”.

    You can always check the permissions of a file using the ls -l command. For example, ls -l

  3. 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.

  1. 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 USERNAME that stores the current user’s username.

    So edit the file so it looks like this:

    echo "Hello, $USERNAME!"

    The $USERNAME syntax is used to access the value of the USERNAME variable.

  2. Run the script. Once you’ve made the changes above, run the modified script.


    You should see this output:

    Hello, tdonohue!

    (Again, or whatever your username is!)

Define our own environment variables

Finally, to finish off, let’s define our own environment variables.

  1. Define an environment variable. Run the following command to define an environment variable called SECRET_CODE:

    export SECRET_CODE="1234"

    The export command is used to define an environment variable. The syntax is export VARIABLE_NAME="value".

    You can also define an environment variable without the export command, but it will only be available in the current shell session. Using export makes it available to all child processes, including the programs you run.

  2. Edit the script to print the environment variable. Now let’s edit the script to use the SECRET_CODE environment variable. Edit the file so it looks like this:

    echo "Hello, $USERNAME!"
    echo "Your secret code is $SECRET_CODE"

    Now when we run the script, it will print the value of the SECRET_CODE environment variable.

  3. 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!

Wrapping up

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 echo command
  • Use environment variables in a shell script

See you in the next tutorial!

Tom Donohue

By Tom Donohue, Editor | Twitter | LinkedIn

Tom is the founder of Tutorial Works. He’s an engineer and open source advocate. He uses the blog as a vehicle for sharing tutorials, writing about technology and talking about himself in the third person. His very first computer was an Acorn Electron.

Join the discussion

Got some thoughts on what you've just read? Want to know what other people think? Or is there anything technically wrong with the article? (We'd love to know so that we can correct it!) Join the conversation and leave a comment.

Comments are moderated.