Kahibaro
Discord Login Register

2.1.4 Environment variables

What environment variables are

Environment variables are named values stored by the shell and the operating system that programs can read. They influence how programs behave without changing the programs themselves.

Examples of common things controlled by environment variables:

They are always strings (text), even if they look like numbers.

Environment variables are different from:

Viewing environment variables

To see environment variables, you can use several commands. They show similar information, but with different scopes.

`printenv`

printenv is a simple, focused tool for environment variables.

Show all environment variables:

printenv

Show a single variable:

printenv PATH
printenv HOME

If a variable is not set, printenv will print nothing and return an error code (useful in scripts).

`env`

env can show, temporarily modify, and run commands with a custom environment.

Show all environment variables:

env

Run a command with a changed environment only for that command:

LANG=fr_FR.UTF-8 env date

After that command finishes, LANG in your shell is unchanged.

`set` (shell built-in, includes more than environment)

In most shells, set shows:

This output is usually long and not beginner-friendly, but it’s good to know:

set

For viewing just environment variables, prefer printenv or env.

Reading the value of a variable

Use $ followed by the variable name:

echo $HOME
echo $USER
echo $PATH

If a variable is not set, echo $SOMETHING prints an empty line.

When variable names are followed by other characters, use {} to make it clear:

echo "Home is ${HOME}dir"   # safer than $HOMEdir

Important common environment variables

You will encounter these often:

These names are conventions; most are widely recognized by many programs.

Setting and changing environment variables (current shell)

There are two steps you need to distinguish:

  1. Setting a shell variable.
  2. Exporting it so that child processes (commands you run) can see it.

Temporary variable in the current shell

In Bash and similar shells:

MYVAR="hello"
echo $MYVAR

MYVAR exists only:

Most simple programs will not see MYVAR until you export it.

Exporting a variable

export marks a variable so it is included in the environment of child processes.

Two common patterns:

  1. Set, then export:
   MYVAR="hello"
   export MYVAR
  1. Set and export in one line:
   export MYVAR="hello"

Now, commands you run can see MYVAR:

export MYVAR="hello"
env | grep MYVAR

To confirm a variable is exported, you can use export with no arguments:

export

This lists exported variables (and functions).

Setting for a single command only

You can prefix a command with variable assignments. They apply only to that command:

LANG=de_DE.UTF-8 date

LANG is changed only for that date process. In your shell:

echo $LANG

remains whatever it was before.

You can set multiple this way:

VAR1=one VAR2=two env | grep '^VAR'

Unsetting environment variables

To remove a variable from the environment and shell:

unset MYVAR

After that:

echo $MYVAR   # prints nothing

unset works for both shell and environment variables.

How environment variables are inherited

Key behavior:

Example:

export CHILDVAR="visible to child"
bash          # start a new shell as child
echo $CHILDVAR

Inside the child shell, $CHILDVAR is still available because it was inherited.

If you set a variable only in the child:

CHILDONLY="hi from child"
exit          # go back to parent shell
echo $CHILDONLY

The parent shell does not see CHILDONLY.

This “one-way inheritance” is fundamental: environment flows from parent to child, not the other way.

Persisting environment variables across sessions

So far, variables disappear when you close the terminal. To keep them across logins, you add them to shell startup files. The exact files differ by shell and distribution, but for Bash you commonly use:

A simple rule of thumb for beginners on typical desktop systems:

Example:

  1. Edit ~/.bashrc:
   nano ~/.bashrc
  1. Add at the end:
   export EDITOR="nano"
   export PATH="$HOME/.local/bin:$PATH"
  1. Apply the changes to your current shell:
   source ~/.bashrc
   # or
   . ~/.bashrc

Now EDITOR and the modified PATH will be set every time a new Bash terminal starts.

System‑wide environment configuration (for all users) is usually done in files like /etc/environment or /etc/profile, but editing those requires sudo and is typically done by administrators.

Working with `PATH`

PATH is one of the most important variables. It controls where the shell looks for commands when you type a name without a path.

The format is:

dir1:dir2:dir3:...

To see it:

echo $PATH

To add a directory to the front of PATH (higher priority):

export PATH="$HOME/bin:$PATH"

To add to the end (lower priority):

export PATH="$PATH:$HOME/bin"

Common beginner mistake: overwriting instead of extending PATH. Avoid:

export PATH="$HOME/bin"   # BAD: you lose all existing entries

Instead, always include the old $PATH unless you know exactly what you’re doing.

Environment variables and security

Environment variables can contain:

Basic safety guidelines:

For everyday beginner use (like setting EDITOR, tweaking PATH), these concerns are minor, but be aware that environment variables are not inherently private or encrypted.

Practice ideas

Try these small exercises to get comfortable:

  1. Show all environment variables, then filter by a keyword:
   printenv | grep HOME
   printenv | grep SHELL
  1. Create and export a variable, then confirm it’s visible to a child shell:
   export MYCOLOR="blue"
   bash
   echo $MYCOLOR
   exit
  1. Temporarily run a command in another language (if locales are installed):
   LANG=fr_FR.UTF-8 date
   LANG=en_US.UTF-8 date
  1. Safely add ~/bin to your PATH in ~/.bashrc, reload it, and confirm:
   echo $PATH
   # edit ~/.bashrc to add:
   # export PATH="$HOME/bin:$PATH"
   source ~/.bashrc
   echo $PATH

These small experiments will make environment variables feel much more intuitive.

Views: 75

Comments

Please login to add a comment.

Don't have an account? Register now!