Home / Blog / Export Command in Linux: A Complete Guide to Environment Variables

Export Command in Linux: A Complete Guide to Environment Variables

Learn how to use the Linux export command to create and manage environment variables, configure application environments, control shell behavior, and pass variables to child processes.

Export Command in Linux: A Complete Guide to Environment Variables

Linux provides many command-line utilities that help developers and system administrators configure and manage their systems. One particularly important command is export, which is used to make shell variables available to processes started from the current shell.

Environment variables are commonly used for application configuration, API settings, executable paths, credentials, deployment environments, and other system-level settings. Understanding the export command is therefore essential for anyone working with Linux, shell scripting, or server environments.

What Is the Export Command in Linux?

The export command is a shell builtin used to mark a variable so that it is included in the environment of subsequently executed child processes.

Basic syntax:

export VARIABLE=value

For example:

export APP_ENV=production

The variable can then be accessed by programs and scripts launched from that shell.

What Are Environment Variables?

Environment variables are named values that provide configuration information to processes running on a system.

Common examples include:

PATH
HOME
USER
SHELL
LANG

Applications can also define their own variables, such as:

DATABASE_URL
API_URL
APP_ENV
NODE_ENV

Environment variables are especially common in web development, DevOps, cloud deployments, and server administration.

Setting an Environment Variable

You can create a shell variable using:

APP_ENV=development

However, this variable is normally available only within the current shell context.

To export it:

export APP_ENV=development

The exported variable becomes available to child processes launched from that shell.

Check an Environment Variable

Use echo to display its value:

echo $APP_ENV

Example output:

development

You can also use:

printenv APP_ENV

This displays the value of the specified environment variable.

Export an Existing Variable

You can first create a variable:

PROJECT_NAME="MyApplication"

Then export it:

export PROJECT_NAME

This marks the existing variable for export.

You can verify it with:

echo $PROJECT_NAME
Export and Child Processes

One of the most important concepts behind export is inheritance.

Suppose you execute:

export APP_ENV=production

Then launch a shell:

bash

Inside the new shell:

echo $APP_ENV

The output will be:

production

This happens because exported variables are inherited by child processes.

Export Multiple Variables

You can export multiple variables separately:

export APP_ENV=production
export APP_PORT=8080
export APP_NAME=myapp

You can then access them:

echo $APP_ENV
echo $APP_PORT
echo $APP_NAME

This is useful when configuring an application's runtime environment.

Export Multiple Variables in One Command

You can also export multiple variables in one command:

export APP_ENV=production APP_PORT=8080 APP_NAME=myapp

This can make shell configuration more concise.

The PATH Environment Variable

One of the most important environment variables in Linux is PATH.

It tells the shell where to look for executable programs.

Display the current PATH:

echo $PATH

You can add a directory to PATH:

export PATH=$PATH:/opt/myapp/bin

Now programs stored in /opt/myapp/bin can potentially be executed without specifying their complete path.

Exporting a Custom Application Path

Suppose your application tools are stored in:

/opt/tools/bin

You can add the directory to PATH:

export PATH=$PATH:/opt/tools/bin

Check the updated value:

echo $PATH

This approach is frequently used when configuring development tools and command-line utilities.

Export in Shell Scripts

The export command is commonly used in shell scripts.

Example:

#!/bin/bash

export APP_ENV=production

echo "Environment: $APP_ENV"

The script sets the environment variable and then uses it.

Exported variables can also be consumed by commands launched by the script.

Passing Variables to Another Script

Suppose you have a script called deploy.sh.

You can set:

export DEPLOY_ENV=production

Then execute:

./deploy.sh

Inside deploy.sh, the variable can be accessed using:

echo $DEPLOY_ENV

This makes environment variables useful for configurable automation scripts.

Export With Node.js Applications

Node.js applications frequently use environment variables for configuration.

For example:

export NODE_ENV=production

A Node.js application can then access the variable through the process environment.

For example:

console.log(process.env.NODE_ENV);

This allows the same application code to operate differently in development, testing, and production environments.

Export With PHP Applications

Environment variables are also useful for PHP applications.

For example:

export APP_ENV=production

A PHP application can access environment variables through PHP's environment mechanisms.

This approach can help keep configuration separate from application source code.

Export for Database Configuration

Database applications often require connection information.

For example:

export DB_HOST=localhost
export DB_PORT=3306
export DB_NAME=myapp

Applications can read these values at runtime.

For sensitive information such as passwords and API keys, use appropriate secret-management practices rather than placing credentials directly into shell history or publicly accessible configuration files.

Export API Configuration

Applications commonly require API endpoints or service configuration.

Example:

export API_URL=https://api.example.com

The application can then read the variable when it starts.

This makes it easier to use different API endpoints across development, testing, and production environments.

Temporary vs Persistent Environment Variables

A variable exported in the current shell is generally temporary.

For example:

export APP_ENV=production

If you close the terminal session, the setting will normally disappear.

If you need a variable to be configured automatically for future shell sessions, it can be added to the appropriate shell startup configuration.

For Bash, this may include:

~/.bashrc

For login environments, depending on the system and shell configuration, files such as:

~/.profile

may also be relevant.

After modifying a shell configuration file, you can reload it:

source ~/.bashrc
Export and Source

The source command can execute commands from a shell configuration file in the current shell.

For example:

source ~/.bashrc

This can apply newly added exported variables without requiring a new terminal session.

Remove an Exported Variable

To remove a variable from the environment:

unset APP_ENV

Then:

echo $APP_ENV

will no longer return the previous value.

Be careful when removing variables used by applications or system scripts.

View Exported Variables

You can use:

export

without specifying a variable to display exported shell variables.

Another useful command is:

printenv

which displays environment variables available to the current process environment.

Export Command and Security

Environment variables are convenient, but they should not automatically be considered a secure secret store.

Avoid exposing sensitive information through:

Public shell scripts
Version-control repositories
Terminal recordings
Debug output
Shared configuration files

For production applications, use dedicated secret-management solutions where appropriate.

Common Mistakes With Export
Forgetting the $ When Reading a Variable

Set a variable:

export APP_ENV=production

Read it using:

echo $APP_ENV

The $ tells the shell that you want the variable's value.

Assuming Export Is Permanent

Running:

export APP_ENV=production

does not automatically make the setting permanent across all future sessions.

Persistent configuration requires an appropriate shell startup file or environment-management mechanism.

Accidentally Overwriting PATH

Avoid replacing PATH without preserving its existing contents.

For example:

export PATH=$PATH:/new/path

is generally safer than unintentionally replacing the entire PATH value.

Exposing Secrets

Avoid placing passwords, private tokens, or API keys directly into commands where they may be stored in shell history or exposed to other processes.

Use appropriate secret-management practices for sensitive production data.

Best Practices
Use descriptive environment variable names.
Understand the difference between shell variables and exported variables.
Avoid exposing sensitive credentials.
Be careful when modifying PATH.
Use shell configuration files for appropriate persistent settings.
Keep production secrets in secure secret-management systems.
Verify variables with echo or printenv.
Use environment variables to separate configuration from application code.
Document important application variables.
Test configuration changes before applying them to production systems.
Export Command in DevOps

The export command is especially useful in DevOps workflows.

It can be used to configure:

Deployment environments
Build processes
Cloud credentials
Application settings
Database connections
API endpoints
CI/CD variables

For example:

export APP_ENV=production
export APP_VERSION=1.5.0

A deployment script can then use these variables to determine how the application should be built or deployed.

Export Command in Docker and Containers

Environment variables are also widely used when working with containers.

For example:

export APP_ENV=production

A container-management workflow can use the variable when starting an application.

This allows deployment configurations to change without modifying application source code.

Export Command in Cloud Environments

Cloud-hosted applications frequently rely on environment variables for runtime configuration.

Variables may define:

Application environments
Service endpoints
Feature flags
Database settings
Regional configuration
External service configuration

Using environment variables can make applications easier to deploy across different environments.

Conclusion

The Linux export command is a simple but important tool for managing environment variables. It allows shell variables to be passed to child processes, making it useful for application configuration, scripting, development, server administration, and DevOps workflows.

From configuring PATH and application environments to passing settings into deployment scripts, understanding export helps Linux users build more flexible and maintainable workflows.

For developers and system administrators, mastering environment variables and the export command is an important step toward effective Linux administration and application deployment.

Contact Us

1119 W Duarte Rd, Arcadia, CA 91007

Solace Infotech Pvt. Ltd, Supreme HQ,
          HQ3C+9F2, Yash Orchid Society,
          Baner, Pune, Maharashtra 411021

4th Floor, Samraat Nucleus,
           Mumbai Naka, Nashik - 422001