export Command: Tutorial & Examples

Set the value of an environment variable for the current shell and child processes

The export command is a vital Unix utility used to set environment variables within your current shell session and make them available to child processes. Environment variables store configuration and control information for the shell and programs it runs, such as the search path for executables ( PATH), user preferences, or system settings. Using export, you can control which variables are inherited by scripts, commands, and programs launched from your shell.

Understanding how export works and how to use it effectively is essential for managing your Linux server environment, automating tasks with scripts, and configuring software behavior.

How export Works

In Linux, variables exist in two main scopes: shell variables and environment variables. Shell variables are local to your current shell session, while environment variables are passed to child processes spawned by the shell.

When you create a variable like this:

    MYVAR="hello"

it is a shell variable and not automatically visible to programs started from that shell. To make it available to child processes, you must "export" it:

    export MYVAR

This marks MYVAR for export, adding it to the environment of any subsequently executed commands. The export command thus bridges the gap between your shell's local variables and the environment of executed programs.

What export Does

The export command does not create variables by itself; it marks existing shell variables to be included in the environment passed to child processes. When you run:

    export MYVAR=value

it creates or updates the shell variable MYVAR with the value value and marks it for export in one step.

Exported variables can be accessed by programs and scripts executed from your shell. This is crucial for configuring programs or scripts that rely on environment variables to modify their behavior.

Why export Is Important

Many programs depend on environment variables for configuration. For example, the PATH variable tells the shell where to look for executables. Without exporting PATH, child processes would not know which directories to search.

Exporting variables is also essential when writing shell scripts or running commands that rely on customized environment settings. Without export, child processes receive only a minimal environment, which can lead to unexpected behavior or errors.

How To Use export

The basic syntax of export is:

    export VARIABLE_NAME=value

or

    export VARIABLE_NAME

to export an already defined shell variable.

You can also export multiple variables at once:

    export VAR1=value1 VAR2=value2

To check which variables are currently exported in your environment, use:

    export -p

which prints all exported variables and their values.

To remove a variable from the exported environment but keep it as a shell variable, use:

    export -n VARIABLE_NAME

Common Command Line Parameters

  • -p
    Prints all exported variables and their values.

  • -n
    Removes the export property from a variable.

  • -f
    Exports shell functions (bash-specific).

Practical Examples Using export

  1. Set and export a variable

    export MYVAR="Hello World"
    echo $MYVAR
    

Output:

    Hello World
  1. Export an existing variable

    MYVAR="New Value"
    export MYVAR
    echo $MYVAR
    

Output:

    New Value
  1. Using export in a script

Create a script showvar.sh:

    #!/bin/bash
    echo "MYVAR is: $MYVAR"

Make it executable and run it:

    chmod +x showvar.sh
    ./showvar.sh

If MYVAR is not exported, the script prints an empty line. After exporting MYVAR:

    export MYVAR="Exported Value"
    ./showvar.sh

Output:

    MYVAR is: Exported Value
  1. Exporting variables with special characters

Use quotes to include spaces or special characters:

    export GREETING="Hello, user!"
    echo $GREETING

Output:

    Hello, user!
  1. View all exported environment variables

    export -p
    

Typical partial output:

    declare -x PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
    declare -x HOME="/root"
    declare -x LANG="en_US.UTF-8"

Common Errors and Troubleshooting

  • Variable not available in child processes
    This usually happens when you define a variable but forget to export it. Always use export if a variable must be accessible to scripts or programs launched from the shell.

  • Syntax errors when exporting
    Avoid spaces around the equal sign. For example, the following is incorrect and will cause an error:

      export MYVAR = value
    

Correct usage:

      export MYVAR=value
  • Variable not visible in a different shell session
    Exported variables exist only in the current shell session and its children. To make variables persist across sessions, define and export them in shell initialization files such as /etc/profile or ~/.bashrc (user-specific).

Security Considerations

Be cautious when exporting variables containing sensitive information such as passwords or API keys. Exported environment variables can be accessible to other processes started by your user or potentially logged by system tools. Use secure methods like configuration files with restricted permissions or secret management tools whenever possible.

Possible Alternatives and Related Commands

  • env — Run a command in a modified environment or show environment variables.
  • printenv — Display environment variables.
  • set — Show shell variables and functions.
  • unset — Remove a shell or environment variable.

See Also

Further Reading

As an Amazon Associate, I earn from qualifying purchases.

The text above is licensed under CC BY-SA 4.0 CC BY SA