Linux

Mastering Variables in Bash (Shell Scripting Guide)

Bash variables are one of the first things you’ll encounter when using Linux — but there’s a big difference between shell variables, environment variables, and how to make them persist globally.

This guide will take you from the basics, with clear examples.


1. What Is a Variable?

A variable is just a named value stored in memory that the shell (or a program) can use.

MY_NAME="Derek"
echo "Hello, $MY_NAME"

Output:

Hello, Derek

No spaces allowed around the equal sign. Quotes are optional unless your value contains spaces.


2. Shell Variables vs. Environment Variables

Here’s where most confusion starts.

Shell Variable

  • Exists only inside your current shell session.
  • Other programs cannot see it.
MY_VAR="test"
echo $MY_VAR      # works
env | grep MY_VAR   # it doesn't exist in the environment variables

Environment Variable

  • Exists in the environment of the shell.
  • Is exported so that all programs you run can see it.
export MY_VAR="test"
env | grep MY_VAR   # shows MY_VAR=test

Now, any command you run (even in that session) can access it.


3. Temporary Variables (Session-Only)

You can set a variable temporarily in two ways:

Option 1: Normal + export

MY_VAR="hello"
export MY_VAR

Or in one line:

export MY_VAR="hello"

This lasts until you close the shell.


Option 2: Inline for One Command

You can set a variable just for a single command:

MY_VAR="hello" some_program

some_program sees MY_VAR, but the variable does not stay in your environment after that command finishes.


4. Reading Variables

You can check what’s currently set:

echo "$MY_VAR"     # prints variable contents
set                # shows all shell variables
env                # shows only exported (environment) variables

5. Unsetting Variables

Remove a variable completely:

unset MY_VAR

6. Making Variables Global (Permanent)

You often want variables to persist across sessions.
To do this, you add them to a shell startup file.

Per-User (Recommended)

Edit ~/.bashrc (or ~/.bash_profile on some systems):

nano ~/.bashrc

Add at the end:

export MY_VAR="hello world"

Then reload:

source ~/.bashrc

Now it will load every time you open a new terminal.


System-Wide (For All Users)

Place them in /etc/environment or /etc/profile.d/custom.sh.

Example /etc/profile.d/myvars.sh:

export COMPANY_NAME="Proport"
export DB_HOST="db.internal"

Save and run:

sudo chmod +x /etc/profile.d/myvars.sh

These will apply to all users on login.


7. Special Built-In Variables

Some variables are set automatically by Bash:

VariableDescription
$HOMEYour home directory
$USERYour username
$PATHDirectories searched for commands
$PWDCurrent working directory
$?Exit status of last command (0 = success)
$$PID of current shell

You can modify some of these (e.g. extend $PATH):

export PATH="$PATH:/opt/my-tools/bin"

8. Best Practices ⭐⭐⭐⭐⭐

Quote your variables – prevents problems with spaces:

echo "$MY_VAR"

Use UPPERCASE for environment variables – by convention.

Don’t over-export – only export what other processes need.

Use inline variables for one-off commands – keeps environment clean.

Store permanent variables in .bashrc – so they survive reboots.


9. Quick Visual Summary

# Local shell variable (not visible to programs)
FOO="bar"

# Make it visible to child processes
export FOO

# Or do it in one line
export FOO="bar"

# Or just for one command
FOO="bar" some_program

10. Debugging Tip

Run a program with env in front to see what variables it sees:

env | grep VAULT

Great for checking if something like VAULT_ADDR or VAULT_NAMESPACE is actually being passed before you run vault.


Final Thoughts

If you keep these rules in mind, you’ll never be confused about why a variable “disappears” between commands or why a program can’t see it.

Think of it like working at McDonald’s:

  • A shell variable is like telling just your team member at the till.
  • An environment variable is like putting it on the kitchen screen for the whole restaurant to see.
  • A permanent variable is like updating the store recipe — every shift after that follows it automatically.

Master these, and you’ve definitely earned your ⭐⭐⭐⭐⭐ badge. 😉