Skip to main content

Overview

The export command sets or modifies environment variables in the current Nash shell session. Variables set with export are available to all commands executed in the current session and any subshells.

Syntax

export KEY=VALUE [KEY=VALUE...]

Description

The export command sets environment variables using KEY=VALUE syntax. Multiple variables can be set in a single command. The variables are immediately available to all subsequent commands and persist for the duration of the shell session.
In Nash, variables must be set with export to be available. Unlike standard bash, Nash does not support setting variables without export.

Options

The export command does not accept any flags or options. It only accepts KEY=VALUE assignments.

Examples

Set a Single Variable

export API_KEY=abc123
echo $API_KEY
# Output: abc123

Set Multiple Variables

export HOST=localhost PORT=8080 DEBUG=true
echo $HOST:$PORT
# Output: localhost:8080

Set Variable with Spaces

export MESSAGE="Hello, World!"
echo $MESSAGE
# Output: Hello, World!

Update Existing Variable

export PATH=/custom/bin
env | grep PATH
# Output: PATH=/custom/bin

Append to PATH

export PATH=$PATH:/new/path
echo $PATH
# Output: /usr/local/bin:/usr/bin:/bin:/new/path

Set Empty Value

export EMPTY=
env | grep EMPTY
# Output: EMPTY=

Use Command Substitution

export CURRENT_DIR=$(pwd)
echo $CURRENT_DIR
# Output: /home/user
export DB_HOST=localhost DB_PORT=5432 DB_NAME=myapp
echo "Database: $DB_HOST:$DB_PORT/$DB_NAME"
# Output: Database: localhost:5432/myapp

Practical Use Cases

Configure Application

# Set application configuration
export APP_ENV=production
export LOG_LEVEL=info
export MAX_CONNECTIONS=100

# Verify settings
env | grep APP

API Configuration

# Set API credentials
export API_URL=https://api.example.com
export API_KEY=secret_key_here
export API_TIMEOUT=30

Development Environment

# Set development variables
export NODE_ENV=development
export DEBUG=true
export PORT=3000

Custom Tool Configuration

# Configure editor
export EDITOR=nano

# Set default pager
export PAGER=less

Build Configuration

# Set build flags
export BUILD_TYPE=release
export TARGET_ARCH=x86_64
export OPTIMIZATION=-O2

Session Customization

# Customize prompt (if supported)
export PS1="[\u@\h \W]\$ "

# Set history size
export HISTSIZE=1000

Variable Naming Conventions

Uppercase
convention
Environment variables are conventionally named in UPPERCASE
export API_KEY=value
export DATABASE_URL=postgres://...
Underscores
convention
Use underscores to separate words, not hyphens or spaces
export MY_VARIABLE=value  # Good
export MY-VARIABLE=value  # Bad (won't work)
No spaces
requirement
No spaces around the equals sign
export KEY=VALUE   # Good
export KEY = VALUE # Bad (won't work)

Variable Expansion

Variables can be referenced in several ways:
export NAME=Alice

# Basic expansion
echo $NAME
# Output: Alice

# Braced expansion (recommended for clarity)
echo ${NAME}
# Output: Alice

# Use in strings
echo "Hello, $NAME!"
# Output: Hello, Alice!

# Use in command substitution
export GREETING="Hello, $(echo $NAME)"
echo $GREETING
# Output: Hello, Alice

Subshell Behavior

Variables are inherited by subshells:
export OUTER=value

# Subshell inherits variable
(echo $OUTER)
# Output: value

# Changes in subshell don't affect parent
(export OUTER=changed; echo $OUTER)
# Output: changed

echo $OUTER
# Output: value (unchanged)

Common Patterns

Load Configuration from File

# config.env contains:
# export API_KEY=abc123
# export API_URL=https://api.example.com

cat config.env
# Then manually export or source if supported

Conditional Export

# Set default if not already set
test -z "$PORT" && export PORT=8080

Export with Fallback

# Use environment variable or default
export LOG_LEVEL=${LOG_LEVEL:-info}
# Note: Parameter expansion may not be fully supported in Nash

Notes

  • Variables persist only for the current shell session
  • Variables are not saved between Nash sessions
  • Use .nashrc to set variables on every startup
  • Variable names are case-sensitive
  • Special characters in values should be quoted
  • Spaces around = will cause syntax errors
  • Variable names cannot start with numbers
  • Avoid overwriting critical system variables like PATH without preserving the original value

Exit Status

The export command always returns exit code 0 (success).
  • env - Display all environment variables
  • unset - Remove environment variables
  • echo - Display variable values
  • test - Test variable values and conditions

Build docs developers (and LLMs) love