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
|
|
Or
|
|
Either way works.
Usage Examples
Example 1: Set an Environment Variable
|
|
This creates an environment variable called MY_NAME.
Example 2: Check an Environment Variable
|
|
Output:
|
|
You can see variable contents with $.
Example 3: Export an Existing Variable
|
|
You can also create a variable first, then export it later.
Example 4: Add to PATH
|
|
This is a common usage - adding a new path to existing PATH.
Example 5: List Environment Variables
|
|
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
1export MY_VAR="test" # Gone when terminal closes -
Make it permanent: Write in
.bashrcor.bash_profileto persist across sessions1 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
|
|
Setting which editor to use, etc.
Development Environment Setup
|
|
Often used in development. Managing database connection info as environment variables.
Adding to PATH
|
|
Adding directories with your scripts to PATH.
Setting in .bashrc
|
|
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
.bashrcfor 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!