The Linux export command: define environment variables

Introduction

In the Linux world and Unix-type systems, environment variables are key-value pairs that influence the behavior of processes and the shell itself. Define them correctly allows you to configure execution routes, program options and system settings without changing the source code. The commandexportis the most used tool to make a variable available in the current environment and in the subprocesses that are launched from it.

What is an environment variable?

An environment variable is simply a name associated with a value that the kernel and programs can consult. Common examples areHOME, PATH, LANGorEDITOR. When a process begins, it inherits the environment of its parent process; any change in that environment only affects the current process and its children, never the father.

The export command: basic syntax

The simplest way to useexportis:

export NOMBRE=valor

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

export MI_VAR='texto con espacios'

It is also possible to export several variables at once:

export VAR1=val1 VAR2=val2

To see the content of a variable after export, just useecho $NOMBREorprintenv NOMBRE.

Practical examples

  • Define a temporary work directory:
    export TMPWORK=$HOME/tmpwork
  • Add a directory to PATH:
    export PATH=$PATH:/opt/miapp/bin
  • Set the location for the messages to appear in Spanish:
    export LANG=es_ES.UTF-8
  • Pass a variable to a subprocess:
    export MI_TOKEN=abc123&& ./mi_script.sh

Make the permanent variables

The effect ofexportlasts only as long as the current shell session exists. For a variable to be available in each new login, the export line should be added to one of the shell configuration files, such as:

  • ~/.bashrcfor Bash (read in interactive shells)
  • ~/.profileor~/.bash_profilefor login shells
  • ~/.zshrcfor Zsh

After editing the file, it is recharged withsource ~/.bashrcor simply opening a new terminal.

Good practices and tricks

  • Use names in capital letters and low scripts to avoid collisions with internal commands.
  • Do not export sensitive data (such as passwords) into files that can be read by other users; prefer secret managers or session variables.
  • If you need to modifyPATH, add the new directory at the end or at the beginning according to the priority you want.
  • For variables that are only relevant in a particular script, it is better to define them within the script itself withexportor just useVAR=valor comandoto affect only that command.
  • Useexport -pto list all the variables currently exported.
  • Remember that the exported variables do not persist in the unless system reboot are saved in the above-mentioned start files.

Conclusion

The commandexportis a key piece for any Linux user or administrator who needs to control the running environment of your programs. Dominating your 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 work is under aCreative Commons License Attribution 4.0 International for Francesc Roig francesc @ vivaldi.net.

EnglishenEnglishEnglish