Setting and Exporting Variables
Learn how to create, modify, and export environment variables to customize your shell environment.
Setting Variables
Basic syntax:
VARIABLE_NAME=value
Important: No spaces around the = sign!
Variable Naming Rules
- Start with letter or underscore
- Can contain letters, numbers, underscores
- Case sensitive (convention: UPPERCASE for env vars)
- No spaces in names
# Good
MY_VAR=value
_private=secret
CONFIG_PATH=/etc
# Bad
my-var=value # Hyphens not allowed
1stVar=value # Can't start with number
my var=value # Spaces not allowed
export - Make Variables Available
export makes variables available to child processes:
MY_VAR="local only" # Shell variable
export MY_VAR # Now exported
export OTHER_VAR="value" # Set and export
Exercise: Set a Variable
Create and display an environment variable:
Viewing Exported Variables
export # List all exports
export -p # Same, with 'declare -x' prefix
Unsetting Variables
Remove a variable:
unset VARIABLE_NAME
Temporary Variables
Set a variable for one command only:
DEBUG=1 ./script.sh
LANG=C sort file.txt
Variable Expansion
Use variables in strings:
echo "Hello, $USER"
echo "Home: ${HOME}/documents"
echo "Date: $(date)"
Modifying Variables
# Append to variable
PATH=$PATH:/new/path
PATH="${PATH}:/another/path"
# Prepend to variable
PATH=/new/path:$PATH
Making Variables Permanent
Add to shell config file:
For Current User
# Add to ~/.bashrc
export MY_VAR="value"
For All Users (requires root)
# Add to /etc/environment
MY_VAR="value"
Common Use Cases
Set Editor
export EDITOR=vim
export VISUAL=vim
Set Language
export LANG=en_US.UTF-8
export LC_ALL=en_US.UTF-8
Application Config
export NODE_ENV=production
export DATABASE_URL="postgres://..."
Exercise: Export a Variable
Export a variable and verify it's in the environment:
Key Takeaways
- Set variables with
NAME=value(no spaces!) - Use
exportto make available to child processes - Use
unsetto remove variables - Add to
~/.bashrcfor persistence - Variables are case-sensitive (use UPPERCASE)

