Skip to content

How to Use export, set and env in Bash (Key Differences)

DodaTech 2 min read

In this tutorial, you'll learn about How to Use export, set and env in Bash (Key Differences). We cover key concepts, practical examples, and best practices to help you understand and apply this topic effectively.

The Problem

You set a variable with MY_VAR=hello in your terminal, but a child script you run prints an empty string for MY_VAR. Or you used export everywhere and now environment variables leak into unrelated commands. The difference between set, export, and env is fundamental to Bash but often misunderstood. export makes variables available to subprocesses, set controls shell attributes, and env runs a command with a modified environment.

Quick Fix

1. Use export to pass variables to child processes

# Set and export in one command
export APP_ENV=production
export DB_URL=postgres://localhost:5432/myapp

# Now child processes inherit these
./deploy.sh

Inside deploy.sh:

echo "Deploying to $APP_ENV"
echo "Database: $DB_URL"

Expected output:

Deploying to production
Database: postgres://localhost:5432/myapp

Without export, APP_ENV and DB_URL would be empty inside deploy.sh.

2. Use export -n to remove a variable from the environment

export SECRET_KEY=abc123
export -n SECRET_KEY
./safe_script.sh  # Does NOT inherit SECRET_KEY

3. Use env for one-off commands

Run a single command with a temporary environment variable:

env DB_URL=localhost:5432 ./migrate.sh

The variable DB_URL is set only for the duration of migrate.sh. The current shell is not affected at all, unlike export.

4. Use set to control shell behavior

# Exit immediately on error
set -e

# Print commands before executing them (debug mode)
set -x

# Treat unset variables as an error
set -u

# Disable options by replacing - with +
set +e +x +u

5. List current variables and their export status

# List all exported environment variables
export -p

# List all shell variables (including non-exported)
set

# List with env (same as export -p)
env

# Check whether a specific variable is exported
declare -p MY_VAR

Expected output (exported):

declare -x MY_VAR="hello"

Expected output (not exported):

declare -- MY_VAR="hello"

6. Export multiple variables at once

export APP_ENV=production \
       DB_URL=postgres://localhost:5432/myapp \
       LOG_LEVEL=info

7. Use declare -x for explicit export in scripts

#!/usr/bin/env bash
declare -x APP_ENV=production
declare -x DB_URL="postgres://localhost:5432/myapp"

This is equivalent to export but makes the intent clearer in longer scripts.

8. Check if a variable name is valid for export

Variable names must match [a-zA-Z_][a-zA-Z0-9_]*. Invalid names:

# These will fail:
export 123VAR=hello
export my-var=hello
export my.var=hello

Prevention

  • Only export variables that child processes actually need — keep everything else local to the shell
  • Use env VAR=value command for one-off command invocations instead of export followed by unset
  • Use set -u in all shell scripts to catch usage of unset variables early at runtime
  • Prefer declare -x or typeset -x for explicit export in scripts for readability
  • Run env or export -p before debugging environment-related issues to see what the current shell exposes

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro