Environment variables let Linux shell sessions 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 the first readable file from ~/.bash_profile, ~/.bash_login, 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.

Steps to set a Linux shell environment variable:

  1. Inspect the current value before changing it.
    $ printf 'APP_ENV=%s\n' "${APP_ENV-}"
    APP_ENV=

    An empty value means the current shell has no definition for APP_ENV.

  2. Set the variable for the current shell session.
    $ export APP_ENV="staging"

    Use uppercase names for custom environment variables and quote the value when it contains spaces or shell metacharacters.

  3. Confirm that the value is exported into the current environment.
    $ printenv APP_ENV
    staging
  4. Verify that child processes inherit the variable from the current shell.
    $ 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.

  5. Check which shell will need a persistent startup-file change.
    $ printf '%s\n' "$SHELL"
    /bin/bash

    Interactive Bash shells usually read ~/.bashrc, login Bash shells use the first readable file from ~/.bash_profile, ~/.bash_login, or ~/.profile, interactive Zsh shells use ~/.zshrc, and login Zsh shells use ~/.zprofile.

  6. Open the startup file for that shell and add or update the export line.
    $ 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.

  7. Start a new shell without inheriting the current value and confirm that the startup file sets it.
    $ env -u APP_ENV bash -ic 'printenv APP_ENV'
    staging

    Use env -u APP_ENV so the test does not pass merely because the new shell inherited APP_ENV from the current shell. Use bash -lc after editing ~/.bash_profile, ~/.bash_login, or ~/.profile, and use zsh -ic or zsh -lc for the equivalent Zsh files.