Skip to main content

Managing Environment Variables

1. Viewing All Variables

Command Description Notes
env Display all environment variables Simple and widely supported
printenv Print all or specified environment variables More portable than env
set Display all shell variables (including environment) Includes shell-specific variables

Example: Using env to view variables

env
# Output includes variables like:
# PATH=/usr/local/bin:/usr/bin:/bin
# HOME=/home/user
# USER=user

Example: Using printenv to view specific variable

printenv HOME
# Output:
# /home/user

2. Displaying Specific Variable

Command Description Example
echo $VARIABLE_NAME Displays the value of a specific environment variable echo $HOME
printenv VARIABLE_NAME Prints the value of a specific environment variable printenv PATH
env | grep VARIABLE_NAME Filters environment variables to show only the specified one env | grep USER

3. Setting Temporary Variables

Command Description Example
export Sets a temporary environment variable for the current shell session export MY_VAR="value"
unset Removes a temporary environment variable from the current shell session unset MY_VAR

Example: Setting and Using a Temporary Variable

export PATH="$PATH:/new/path"
echo $PATH
Note: These variables are only available in the current shell session and will not persist after closing the terminal.

4. Unsetting Variables

Command Description Notes
unset unset VAR_NAME Removes the variable from the environment
unset -f unset -f function_name Unsets a function definition
unset -v unset -v VAR_NAME Unsets a variable (explicit)

Example: Unsetting a Variable

export MY_VAR="test"
echo $MY_VAR
# Output: test

unset MY_VAR
echo $MY_VAR
# Output: (empty)
Note: Using unset only affects the current shell session and does not persist across new sessions.

5. Setting Permanent Variables

File Content Description
~/.bashrc export MY_VAR="value" Applies to bash shell sessions
~/.profile export MY_VAR="value" Applies to login shells and GUI sessions
/etc/environment MY_VAR=value System-wide environment variables (requires PAM)
/etc/profile.d/custom.sh export MY_VAR="value" System-wide script for custom variables

Example: Adding a variable to ~/.bashrc

echo 'export PATH="$PATH:/usr/local/bin"' >> ~/.bashrc
Note: Changes to ~/.bashrc take effect in new terminal sessions or after running source ~/.bashrc.
Warning: Modifying /etc/environment may require root privileges and can affect all users.

6. Setting System-Wide Variables

File Content Description
/etc/environment PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin" System-wide environment variables for login shells
/etc/profile export NODE_ENV=production Global shell initialization script for login shells
/etc/bash.bashrc alias ll='ls -la' System-wide bash configuration for interactive shells

Example: Adding a system-wide variable

# Edit /etc/environment
echo "JAVA_HOME=/usr/lib/jvm/java-17-openjdk" | sudo tee -a /etc/environment
Note: Changes to /etc/environment require a new login or reboot to take effect.
Warning: Modifying system files directly can affect all users and should be done carefully.

7. Managing PATH Variable

Operation Description Command Notes
View PATH Display current PATH variable echo $PATH Shows all directories where commands are searched
Add to PATH Add a new directory to PATH permanently export PATH=$PATH:/new/directory Appends to current session; use ~/.bashrc or ~/.zshrc for persistence
Remove from PATH Remove a directory from PATH export PATH=$(echo $PATH | sed 's/\/old\/directory://g') Uses sed to remove specific path from list
Reset PATH Restore default PATH export PATH=/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin Typical default PATH on most Linux systems

Example: Adding a Custom Binary Directory to PATH

mkdir -p ~/bin
echo 'export PATH=$PATH:~/bin' >> ~/.bashrc
source ~/.bashrc
Note: Changes to PATH in current session only last until shell is closed. To make them permanent, update shell config files like ~/.bashrc, ~/.zshrc, or /etc/environment.

8. Using Variable Substitution

Feature Description Example
Variable Expansion Expands variables using ${VAR} syntax echo "Hello ${USER}"
Default Value Provides default if variable is unset or empty echo "${HOME:-/tmp}"
Alternate Value Uses alternate value if variable is unset or empty echo "${USER:-$(whoami)}"
Variable Length Expands to the length of a variable's value echo "${#USER}"
Substring Extraction Extracts substring using ${VAR:start:length} echo "${USER:0:3}"
Variable Replacement Replaces all occurrences of a substring echo "${USER//u/U}"

Example: Variable Substitution in Shell Script

#!/bin/bash
name="John"
echo "Hello ${name}, welcome to ${HOME}"
echo "Your home directory is ${HOME:-/tmp}"
echo "First letter of name: ${name:0:1}"

9. Setting Variables in Scripts

Method Description Example
export Sets and exports a variable to child processes export MY_VAR="value"
local Declares a variable local to a function (bash/zsh)
function myfunc() {
  local localVar="localValue"
}
declare Declares a variable with optional attributes (bash) declare -r readonlyVar="value"

Example: Setting and Using Variables in a Script

#!/bin/bash
export DATABASE_URL="postgresql://localhost:5432/mydb"
export API_KEY="abc123"

echo "Database URL: $DATABASE_URL"
echo "API Key: $API_KEY"
Note: Variables set without export are only available in the current shell session and not inherited by child processes.

10. Exporting Variables to Child Processes

Command Description Example
export Exports a variable to child processes export MY_VAR="value"
env Runs a command with environment variables set env VAR=value command

Example: Setting and Exporting a Variable

export PATH="$PATH:/usr/local/bin"
echo $PATH

Example: Running Command with Temporary Variable

env DEBUG=true npm start

Was this article helpful?