export - Linux


Overview

The export command in Linux is used to set or modify environment variables in the shell. Its primary function is to ensure that child processes of a shell inherit the environment variables. This command is crucial for configuring system behavior from a script or terminal, controlling the operational environment for software processes.

Syntax

The basic syntax for the export command is as follows:

export [OPTIONS] [NAME[=VALUE]...]
  • NAME is the name of the environment variable.
  • VALUE is the value assigned to the variable. If not specified, the variable retains its existing value, or it is exported without a value if it was not previously set.

Options/Flags

export does not have a wide range of options. Here are the most commonly used forms:

  • -p: List all names that are exported in the current shell. If used without arguments, it displays the names and values of all environment variables.

Examples

  1. Setting a New Variable

    export PATH=$PATH:/opt/newpath
    

    This command adds /opt/newpath to the end of the existing PATH environment variable.

  2. Exporting Variables

    NAME="John"
    export NAME
    

    This sequence sets NAME as “John” and then exports it to make it available to child processes.

  3. Listing Exported Variables

    export -p
    

    This displays all exported variables and their values in the current shell session.

Common Issues

  • Not Seeing Changes in Another Terminal: Variables exported in one terminal are not available in another terminal window. Each shell session maintains its own set of environment variables.

  • Syntax Errors: Users may encounter errors if the syntax used is incorrect, particularly if spaces are placed around the equals sign (=) when setting a variable value.

Integration

export can be combined with other commands for script-based configurations or startup files like .bashrc or .profile. For example:

#!/bin/bash
export PATH=$PATH:/usr/local/bin
java_application

This script temporarily updates the PATH for its execution and any called child processes like java_application but does not affect the global system environment permanently.

  • env: Displays all exported variables or runs another utility in an altered environment without modifying the current one.
  • set: Without arguments, lists all shell variables, not just those that are exported.
  • unset: Deletes both shell and environment variables.

For further reading, consult the official Linux command-line documentation available on most systems through the man command or the GNU Core Utilities page.