Home > Environment & Utility > export

export: Setting Environment Variables

The `export` command makes shell variables available to child processes, effectively turning them into environment variables. This is useful when scripts or programs need to access specific values.

Overview

`export` promotes shell variables to environment variables, which are then inherited by child processes. This is essential for controlling program behavior, adding directories to the PATH, or passing specific configuration values.

Key Features

  • Inherits variables to child processes
  • Used for setting system variables like PATH
  • Valid only within the current shell session (not permanent)
  • Checks the list of currently exported variables

Main Options

The `export` command is primarily used for variable assignment, but specific flags can enable additional functionalities.

Variable Management and Output

Generated command:

Try combining the commands.

Description:

`export` Executes the command.

Combine the above options to virtually execute commands with AI.

Usage Examples

Various ways to set and manage environment variables using the `export` command.

Setting a New Environment Variable

export MY_VARIABLE="Hello World"

Creates a new environment variable `MY_VARIABLE`, assigns a value, and then exports it.

Exporting an Existing Variable

EXISTING_VAR="Existing Value"
export EXISTING_VAR

Exports an already defined shell variable `EXISTING_VAR` as an environment variable.

Adding a Directory to PATH

export PATH="/usr/local/bin/custom:$PATH"

Adds a new directory `/usr/local/bin/custom` containing executable files to the `PATH` environment variable. The existing `PATH` value is preserved.

Checking Exported Variables List

export -p

Displays a list of all environment variables and functions currently exported in the shell.

Unexporting a Variable

export -n MY_VARIABLE

Removes `MY_VARIABLE` from the export list, so it is no longer inherited by child processes.

Tips & Precautions

Useful tips and points to consider when using the `export` command.

Permanent Environment Variable Settings

The `export` command is only effective for the current shell session. To make environment variables persist across system reboots or apply to all new shells, you need to add the `export` command to configuration files such as `.bashrc`, `.profile`, or `.zshrc` in your home directory, or to system-wide configuration files (e.g., `/etc/profile`, `/etc/environment`).

  • User-specific settings: ~/.bashrc, ~/.profile, ~/.zshrc
  • System-wide settings: /etc/profile, /etc/environment

Child Process Inheritance

Environment variables are inherited from the parent shell to the child shell. This means variables set with `export` can be accessed by all scripts or programs executed from the current shell. However, environment variables set in a child shell are not passed back to the parent shell.

Security Considerations

While setting sensitive information (e.g., API keys, passwords) as environment variables is convenient, it poses a security risk as other users or processes on the system might be able to access them. Be mindful of potential exposure, especially since process environments can often be viewed with commands like `ps aux`.


Same category commands