Skip to content
# Automation & security

Environment variables

Set variables for your session, export them to child processes, and make them persist.

Shell variable vs environment variable

A plain variable only exists in your current shell. export promotes it to the environment, meaning any command or script you run from that shell inherits it too.

bash
MY_VAR=1        # only visible in this shell
export MY_VAR=1 # visible to child processes too

Inspecting the environment

env or printenv lists everything currently exported; echo $VAR_NAME checks one specific variable.

bash
env | sort
echo $PATH

Making it permanent

A variable set on the command line disappears when the shell closes. To persist it, add the export line to ~/.bashrc (runs for every new interactive shell) or ~/.profile (runs once at login) and reload it or open a new session.

bash
echo 'export EDITOR=nano' >> ~/.bashrc
source ~/.bashrc

System-wide variables

For every user on the box, not just yours, use /etc/environment (simple KEY=value pairs, no export keyword, no logic) instead of editing every user's dotfiles.

text
# /etc/environment
EDITOR=nano