DEV Community

Zach Gollwitzer
Zach Gollwitzer

Posted on • Updated on • Originally published at fullstackfoundations.com

Bash Shell Variables and Aliases (and how to setup your $PATH variable)

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"
Enter fullscreen mode Exit fullscreen mode

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

echo $MY_VARIABLE # some value
Enter fullscreen mode Exit fullscreen mode

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"
Enter fullscreen mode Exit fullscreen mode

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

env
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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

export MY_SHELL_VARIABLE
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Add the following to my-simple-script.

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

Now make the script executable.

chmod 775 my-simple-script
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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

source ~/.profile
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Top comments (1)

Collapse
 
jinglf000 profile image
jing

nice!!!
very clear for new sheller;