Bash Path Variable: How to Set it

One of the most important components to a good development environment is a properly configured $PATH variable. Learn about it in this post.

Written by Zach

Last Updated: Apr 03, 2024

Course Navigation

Loading course...

An environment variable can come in two sizes--global or local. A global environment variable is one that is set upon initialization of a shell and can be used across all your shells. A local environment variable is one that is set during a shell session and is erased when the shell is closed. To create a local environment variable, just export it in your shell.

export MY_VARIABLE="some value"

You can use this as long as the shell is still open.

echo $MY_VARIABLE # some value

As soon as you close the shell, this will no longer be a variable. If you want to create a global variable, you will need to export it in one of your bash configuration files. It is recommended that you place it in the ~/.profile file as described in this tutorial.

# ~/.profile

export GLOBAL_VARIABLE="some value"

To see all of the environment variables in your current environment (both local and global), you can type the following command.

env

Shell Variables

Oftentimes, users will get confused when talking about environment variables and shell variables. Regardless of whether the environment variable is local or global, all the scripts running in that shell session will have access to the variable. With a shell variable, other scripts will not have access to it. The following is how we declare a shell variable.

MY_SHELL_VARIABLE="some value"

echo $MY_SHELL_VARIABLE  # some value

This variable will not be available to any script until we export it and make it an environment variable.

export MY_SHELL_VARIABLE

PATH variable

The most important global environment variable that you must set is the PATH variable. This is the variable that tells the bash shell where to find different executable files and scripts. The shell will check the directories listed in the PATH variable for the script you are trying to find. Here is an example path.

export PATH=/usr/local/bin:/usr/bin:/bin

With this example path, when you try to run a script in the terminal such as cat, bash will first look in the /usr/local/bin for this command. If it doesn't find it there, it will look in /usr/bin, and finally, /bin. If you need to modify the path, you can update it in your ~/.profile like so:

export PATH=/home/zach/script-location:$PATH

This will add the /home/zach/script-location directory to the beginning of the PATH variable. If you want to add it at the end, you can change the line to this:

export PATH=$PATH:/home/zach/script

With this new knowledge, we can create our own scripts and add those scripts to our path so we can run them automatically. Let's create a directory in our home folder that will hold all our custom scripts.

cd ~
mkdir bin
touch bin/my-simple-script

Add the following to my-simple-script.

#!/bin/bash
echo "I am a useless script for tutorial purposes only"

Now make the script executable.

chmod 775 my-simple-script

We need to add the new bin/ directory to our path. Open ~/.profile (or wherever you define your PATH environment variable), and add the following line.

export PATH=$PATH:~/bin

Finally, you need to source ~/.profile to activate this new change.

source ~/.profile

You can now run your simple script from the command line by typing the name of it.

my-simple-script
# I am a useless script for tutorial purposes only