Skip to main content

Environment variables


Table of contents

  1. What is an environment variable?
  2. Common variables
  3. export and persistence
  4. Configuration files
  5. Hands-on exercises


1 - What is an environment variable?

Definition

An environment variable is a dynamic value that affects the behavior of processes.

Usage

Environment variables are used to:

  • Configure applications
  • Store paths
  • Pass sensitive information (tokens, keys)
  • Customize the shell's behavior

Display the variables

# Afficher toutes les variables
env
# ou
printenv

# Afficher une variable spécifique
echo $HOME
echo $PATH
printenv HOME

# Afficher avec détails
env | grep -i path

Create a variable

# Variable locale (shell courant uniquement)
MA_VARIABLE="valeur"
echo $MA_VARIABLE

# Variable d'environnement (héritée par les processus enfants)
export MA_VARIABLE="valeur"
echo $MA_VARIABLE

🔝 Back to table of contents



2 - Common variables

System variables

VariableDescriptionExample
HOMEHome directory/home/john
USERUsernamejohn
SHELLDefault shell/bin/bash
PATHPaths of executables/usr/bin:/bin
PWDCurrent directory/home/john/projet
OLDPWDPrevious directory/home/john
TERMTerminal typexterm-256color
LANGLanguage/localefr_FR.UTF-8
HOSTNAMEMachine nameubuntu-server

The PATH variable

PATH defines where the shell looks for commands.

# Afficher le PATH
echo $PATH
# /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

# Le shell cherche dans l'ordre :
# 1. /usr/local/sbin
# 2. /usr/local/bin
# 3. /usr/sbin
# 4. /usr/bin
# 5. /sbin
# 6. /bin

Add a directory to the PATH

# Ajouter à la fin du PATH
export PATH="$PATH:/mon/chemin"

# Ajouter au début (prioritaire)
export PATH="/mon/chemin:$PATH"

# Exemple : ajouter ~/.local/bin
export PATH="$HOME/.local/bin:$PATH"

Variables for applications

VariableUsage
EDITORDefault editor
VISUALVisual editor
PAGERPagination program
JAVA_HOMEJava installation
NODE_ENVNode.js environment
DATABASE_URLDatabase connection
# Définir l'éditeur par défaut
export EDITOR=vim
export VISUAL=vim

# Configuration application
export DATABASE_URL="postgresql://user:pass@localhost:5432/db"
export NODE_ENV="production"

🔝 Back to table of contents



3 - export and persistence

Local vs exported variable

# Variable locale (uniquement dans le shell courant)
LOCAL_VAR="local"

# Variable exportée (héritée par les sous-processus)
export EXPORTED_VAR="exported"

# Test
bash -c 'echo "Local: $LOCAL_VAR, Exported: $EXPORTED_VAR"'
# Résultat : Local: , Exported: exported

Delete a variable

# Supprimer une variable
unset MA_VARIABLE

# Vérifier
echo $MA_VARIABLE
# (vide)

Variable persistence

Variables defined in the terminal are temporary. To make them permanent:

🔝 Back to table of contents



4 - Configuration files

User files

FileWhen it is read
~/.bashrcEvery new interactive terminal
~/.bash_profileLogin (login shell)
~/.profileLogin (if .bash_profile is absent)
~/.bash_logoutLogout

System files

FileScope
/etc/environmentSystem variables (all sessions)
/etc/profileLogin script (all users)
/etc/profile.d/*.shAdditional scripts
/etc/bash.bashrcGlobal bashrc

Modify ~/.bashrc

# Ouvrir le fichier
nano ~/.bashrc

# Ajouter à la fin :
export EDITOR=vim
export PATH="$HOME/.local/bin:$PATH"
export MY_APP_KEY="secret123"

# Recharger
source ~/.bashrc
# ou
. ~/.bashrc

Modify /etc/environment (system)

# Ouvrir le fichier
sudo nano /etc/environment

# Format simple (pas de export)
PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
JAVA_HOME="/usr/lib/jvm/java-17-openjdk-amd64"

Create a file in /etc/profile.d/

# Créer un fichier personnalisé
sudo nano /etc/profile.d/custom.sh

# Contenu
export COMPANY_NAME="MyCompany"
export APP_ENV="production"

# Rendre exécutable
sudo chmod +x /etc/profile.d/custom.sh

Loading order (login shell)

Loading order (non-login)

tip

For variables you want available everywhere, add them to ~/.bashrc (user) or /etc/environment (system).

🔝 Back to table of contents



5 - Hands-on exercises

Exercise 1: Explore the variables

# 1. Affichez votre HOME
echo $HOME

# 2. Affichez votre PATH
echo $PATH

# 3. Comptez les répertoires dans PATH
echo $PATH | tr ':' '\n' | wc -l

# 4. Affichez toutes les variables commençant par "L"
env | grep "^L"

Exercise 2: Create and export

# 1. Créez une variable locale
MESSAGE="Hello"
echo $MESSAGE

# 2. Vérifiez qu'elle n'est pas héritée
bash -c 'echo $MESSAGE'

# 3. Exportez-la
export MESSAGE

# 4. Vérifiez qu'elle est maintenant héritée
bash -c 'echo $MESSAGE'

# 5. Supprimez-la
unset MESSAGE

Exercise 3: Modify the PATH

# 1. Créez un dossier pour vos scripts
mkdir -p ~/bin

# 2. Ajoutez-le au PATH temporairement
export PATH="$HOME/bin:$PATH"

# 3. Créez un script de test
echo '#!/bin/bash
echo "Mon script fonctionne !"' > ~/bin/monscript
chmod +x ~/bin/monscript

# 4. Testez
monscript

# 5. Pour le rendre permanent, ajoutez dans ~/.bashrc :
# export PATH="$HOME/bin:$PATH"

Exercise 4: Permanent configuration

# 1. Ouvrez ~/.bashrc
nano ~/.bashrc

# 2. Ajoutez ces lignes à la fin :
# # Mes variables personnalisées
# export EDITOR=nano
# export MY_PROJECT_DIR="$HOME/projets"

# 3. Sauvegardez et rechargez
source ~/.bashrc

# 4. Vérifiez
echo $EDITOR
echo $MY_PROJECT_DIR

Quiz

Q1. What is the difference between a variable with and without export?

Answer

Without export: local variable, not inherited by child processes. With export: environment variable, inherited by child processes.

Q2. Where do you add a variable so it is permanent for the user?

Answer

In ~/.bashrc (or ~/.profile for login shells)

Q3. How do you reload ~/.bashrc without opening a new terminal?

Answer

source ~/.bashrc or . ~/.bashrc

🔝 Back to table of contents



Key takeaways

  • Local variables = current shell only
  • export = inherited by child processes
  • PATH defines where to look for commands
  • ~/.bashrc = permanent user configuration
  • /etc/environment = permanent system configuration
  • source ~/.bashrc to reload without restarting
  • unset VAR to delete a variable

🔝 Back to table of contents


← Previous chapter | Next chapter: Redirections and pipes →