How to set environment variables in Linux command line

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.

Steps to set environment variables in Linux command line:

  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 current shell now sees the exported value.
    $ printf '%s\n' "$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 ~/.bash_profile 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.

    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.

  7. Start a new shell that reads the edited startup file and confirm that the value persists.
    $ 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.