Introduction

Hello! In this article, we’ll learn about the export command.

export is a command for setting environment variables, but at first you might think “What are environment variables?” I was confused too when I started.

But once you get used to it, it’s pretty simple, so let’s take a look together.

What is the export Command?

export is a command that sets a shell variable as an environment variable.

Let me explain - when you just create a variable normally, that variable can only be used within the current shell. But if you export it, programs launched from that shell can use it too.

In other words, it’s a command that says “Make this variable available in child processes too.”

Basic Syntax

1
export VARIABLE=value

Or

1
2
VARIABLE=value
export VARIABLE

Either way works.

Usage Examples

Example 1: Set an Environment Variable

1
export MY_NAME="Taro"

This creates an environment variable called MY_NAME.

Example 2: Check an Environment Variable

1
2
export MY_NAME="Taro"
echo $MY_NAME

Output:

1
Taro

You can see variable contents with $.

Example 3: Export an Existing Variable

1
2
MY_VAR="test"
export MY_VAR

You can also create a variable first, then export it later.

Example 4: Add to PATH

1
export PATH="$PATH:/new/path"

This is a common usage - adding a new path to existing PATH.

Example 5: List Environment Variables

1
export

Running without options displays all currently set environment variables.

Commonly Used Environment Variables

Variable Description
PATH List of directories to search for commands
HOME Home directory path
USER Current user name
SHELL Path to the shell being used
LANG Language setting

These are often already set.

Tips & Notes

  • Temporary setting: Closes when you close the terminal

    1
    
    export MY_VAR="test"  # Gone when terminal closes
    
  • Make it permanent: Write in .bashrc or .bash_profile to persist across sessions

    1
    2
    
    # Add to ~/.bashrc
    export MY_VAR="test"
    
  • Difference without export: Without export, child processes can’t use it

    1
    2
    3
    4
    5
    6
    7
    
    # Without export
    MY_VAR="test"
    bash -c 'echo $MY_VAR'  # Nothing displayed
    
    # With export
    export MY_VAR="test"
    bash -c 'echo $MY_VAR'  # test is displayed
    
  • Be careful with overwrites: Overwriting existing environment variables can’t be undone easily

    1
    2
    3
    4
    5
    
    # Completely overwrite PATH (dangerous!)
    export PATH="/new/path"
    
    # Add to PATH (safe)
    export PATH="$PATH:/new/path"
    

Practical Usage

Program Configuration

1
export EDITOR="vim"

Setting which editor to use, etc.

Development Environment Setup

1
2
3
export NODE_ENV="development"
export DB_HOST="localhost"
export DB_PORT="5432"

Often used in development. Managing database connection info as environment variables.

Adding to PATH

1
export PATH="$HOME/bin:$PATH"

Adding directories with your scripts to PATH.

Setting in .bashrc

1
2
3
4
# ~/.bashrc
export LANG=en_US.UTF-8
export EDITOR=vim
export PATH="$HOME/bin:$PATH"

Writing in .bashrc like this sets them automatically on login.

Summary

In this article, we learned about the export command.

Key Points:

  • Set environment variables with export
  • Variables become available in child processes
  • Goes away when terminal closes, write in .bashrc for persistence
  • Often used for PATH additions

Might be a bit confusing at first, but you’ll get used to it with use. Especially in development, it’s a frequently used command, so worth remembering.

Keep learning Linux commands!