Environment variables let command-line sessions in Linux carry settings such as service endpoints, editor choices, locale overrides, and feature flags without hard-coding those values into scripts or config files. Setting the right variable once keeps later commands consistent and makes it easier to switch between environments such as staging and production.
Shells such as Bash and Zsh keep variables in the current process and pass exported values to child processes started from that shell. The export builtin marks a name for inheritance, so commands launched afterward can read the value from their environment. Persisting a variable across new sessions means adding the export to a startup file that the shell actually reads when it starts.
The startup file matters. Interactive Bash shells usually read ~/.bashrc, login Bash shells use ~/.bash_profile or ~/.profile, and Zsh uses ~/.zshrc for interactive shells or ~/.zprofile for login shells. Desktop applications started outside the terminal may not read those files at all, so this workflow targets command-line shells rather than system-wide desktop session settings. Replace old assignments instead of stacking duplicates, and keep secrets out of general shell startup files because exported values can surface in logs, crash dumps, or diagnostic output.
$ printf 'APP_ENV=%s\n' "${APP_ENV-}" APP_ENV=
An empty value means the current shell has no definition for APP_ENV.
$ export APP_ENV="staging"
Use uppercase names for custom environment variables and quote the value when it contains spaces or shell metacharacters.
$ printf '%s\n' "$APP_ENV" staging
$ sh -c 'printf "%s\n" "$APP_ENV"' staging
An exported variable is passed to commands started from the shell, while a non-exported shell variable stays local to the current shell process.
$ printf '%s\n' "$SHELL" /bin/bash
Interactive Bash shells usually read ~/.bashrc, login Bash shells use ~/.bash_profile or ~/.profile, interactive Zsh shells use ~/.zshrc, and login Zsh shells use ~/.zprofile.
$ nano ~/.bashrc
export APP_ENV="staging"
If the file already contains an APP_ENV assignment, replace the old line instead of leaving multiple conflicting definitions in the same startup path.
Use ~/.profile or ~/.bash_profile when the value must appear in new login Bash shells, and use ~/.zshrc or ~/.zprofile for the equivalent Zsh startup files.
$ bash -ic 'printf "%s\n" "$APP_ENV"' 2>/dev/null staging
Use bash -lc after editing ~/.profile or ~/.bash_profile, and use zsh -ic or zsh -lc for the equivalent Zsh files.