The export command in Linux: defining environment variables

Introduction

In the world of Linux and Unix-like systems, environment variables are key‑value pairs that influence the behavior of processes and the shell itself. Defining them correctly allows configuring execution paths, program options, and system settings without modifying source code. The export command is the most commonly used tool to make a variable available in the current environment and in subprocesses spawned from it.

What is an environment variable?

An environment variable is simply a name associated with a value that the kernel and programs can query. Common examples are HOME, PATH, LANG, or EDITOR. When a process starts, it inherits the environment from its parent process; any change made to that environment only affects the current process and its children, never the parent.

The export command: basic syntax

The simplest way to use export is:

export NAME=value

If the value contains spaces or special characters, it should be enclosed in quotes:

export MY_VAR='text with spaces'

It is also possible to export several variables at once:

export VAR1=val1 VAR2=val2

To view the content of a variable after exporting it, simply use echo $NAME or printenv NAME.

Practical examples

  • Define a temporary working directory:
    export TMPWORK=$HOME/tmpwork
  • Add a directory to PATH:
    export PATH=$PATH:/opt/myapp/bin
  • Set the locale so messages appear in Spanish:
    export LANG=es_ES.UTF-8
  • Pass a variable to a subprocess:
    export MY_TOKEN=abc123 && ./my_script.sh

Making variables permanent

The effect of export lasts only while the current shell session exists. To have a variable available in each new login session, you must add the export line to one of the shell’s configuration files, such as:

  • ~/.bashrc for Bash (read in interactive shells)
  • ~/.profile or ~/.bash_profile for login shells
  • ~/.zshrc for Zsh

After editing the file, reload it with source ~/.bashrc or simply open a new terminal.

Best practices and tips

  • Use uppercase names with underscores to avoid collisions with internal commands.
  • Do not export sensitive data (such as passwords) in files that could be read by other users; prefer secret managers or session variables.
  • If you need to modify PATH, add the new directory at the end or beginning according to the priority you desire.
  • For variables that are only relevant in a specific script, it is better to define them inside the script itself with export or simply use VAR=value command so they affect only that command.
  • Use export -p to list all currently exported variables.
  • Remember that exported variables are not persisted across system reboots unless saved in the startup files mentioned above.

Conclusion

The export command is a fundamental piece for any Linux user or administrator who needs to control the execution environment of their programs. Mastering its syntax, knowing when and how to make it permanent, and applying good practices will allow you to work more safely, predictably, and efficiently on the command line.

This post is also available in ESPAÑOL.

Leave a Reply

Your email address will not be published. Required fields are marked *

Esta obra está bajo una Licencia Creative Commons Atribución 4.0 Internacional para Francesc Roig francesc@vivaldi.net .