In Linux, environment variables provide a flexible mechanism for configuring shells and applications without hard-coding settings. Typical values include search paths, default editors, locale definitions, and endpoints required by command-line tools. Consistent environment variable definitions keep scripts predictable across sessions and machines.

When a shell such as Bash or Zsh starts, it builds an environment from the parent process and any startup configuration files. The export builtin associates a name with a value in the current shell environment and marks it for inheritance by child processes, such as scripts launched from the terminal. Commands, interpreters, and GUI applications started from that shell can then read those variables to adjust behavior.

Environment variables can be defined only for the lifetime of a single shell or persisted across sessions by adding export lines to startup files such as ~/.bashrc, ~/.zshrc, or ~/.profile. Editing these files requires care, because syntax errors or conflicting assignments can interfere with login behavior or break existing scripts. Storing secrets in environment variables can also increase exposure through process listings, core dumps, or configuration backups, so sensitive values benefit from additional hardening.

Steps to define environment variables in Linux command line:

  1. Open a terminal window running an interactive Linux shell.
  2. Define a temporary environment variable using the export command.
    $ export VARIABLE_NAME="value"

    Replace VARIABLE_NAME with a descriptive uppercase name and quote the value when it contains spaces or special characters.

  3. Verify that the variable is set in the current shell environment.
    $ echo "$VARIABLE_NAME"
    value

    The shell expands $VARIABLE_NAME and prints the value currently stored in the environment.

  4. Check that the variable is visible to child processes started from the shell.
    $ env | grep VARIABLE_NAME
    VARIABLE_NAME=value

    The env command prints the full environment; filtering with grep confirms that the variable is exported.

  5. Open the startup configuration file for Bash in a text editor to make the variable persistent.
    $ nano ~/.bashrc

    For Zsh, edit ~/.zshrc instead; for login shells that do not source ~/.bashrc, use ~/.profile.

    Introducing a syntax error in startup files can prevent the shell from loading correctly or hide environment variables used by existing scripts.

  6. Add an export line that defines the variable in the startup file.
    export VARIABLE_NAME="value"

    Placing export lines near the end of ~/.bashrc or ~/.zshrc keeps variable definitions easy to find and adjust.

  7. Save the modified configuration file in the editor.

    In nano, press Ctrl+O to write the file, press Enter to confirm, and press Ctrl+X to exit.

  8. Reload the startup file in the current shell so the new variable becomes active immediately.
    $ source ~/.bashrc

    The shorthand form . ~/.bashrc performs the same action as source in most POSIX-compliant shells.

  9. Confirm that the variable is now available and matches the expected value.
    $ echo "$VARIABLE_NAME"
    value
  10. Open a new terminal or start a new shell to confirm that the variable persists across sessions.
    $ echo "$VARIABLE_NAME"
    value

    If the value is missing in the new shell, re-check the startup file and ensure the export line is not inside a conditional block that fails.

  11. Remove the variable from the current shell when it should no longer be present.
    $ unset VARIABLE_NAME

    Unsetting a variable in the current shell does not remove it from startup files, so any persistent definition remains in effect for future sessions until edited.

Discuss the article:

Comment anonymously. Login not required.