When working with Linux systems and bash shells, environment variables play a crucial role in configuration and process communication. The .bashrc
file is executed for interactive non-login shells, making it an ideal place to set user-specific environment variables.
The key difference between these two forms lies in variable scoping:
# This makes VAR available to child processes
export VAR=value
# This sets VAR only in the current shell
VAR=value
The export
command marks the variable for automatic export to the environment of subsequently executed commands. Without it, the variable remains local to the current shell session.
Consider this example where we want to set a Java home path:
# Without export - won't be available to Java processes
JAVA_HOME=/usr/lib/jvm/java-11-openjdk
# With export - will be properly inherited
export JAVA_HOME=/usr/lib/jvm/java-11-openjdk
Use the non-exported form for:
- Temporary variables in scripts
- Variables only needed in the current shell
Use the exported form for:
- Configuration needed by applications (PATH, JAVA_HOME, etc.)
- Variables needed in child processes
- Persistent environment settings
For most environment variable settings in .bashrc
, you should use:
export VAR=value
This ensures the variables will be available to all programs launched from your shell. If you're setting variables that are only needed for shell configuration (like PS1 or shell options), you can omit the export.
To check if a variable is exported:
# Shows all exported variables
export -p
# Check a specific variable
declare -p VAR
Remember to either start a new shell or source your changes after modifying .bashrc
:
source ~/.bashrc
When working with environment variables in Linux/Unix systems, the .bashrc
file is one of the most common places to define them. However, there's a crucial difference between these two approaches:
export VAR=value
VAR=value
The export
command makes the variable available to child processes of the current shell session. Without it, the variable is only available in the current shell:
# This variable won't be available to child processes
LOCAL_VAR="hello"
# This variable will be available to all child processes
export GLOBAL_VAR="world"
Let's test this with a simple script example. First, create a test script:
#!/bin/bash
echo "Local variable: $LOCAL_VAR"
echo "Global variable: $GLOBAL_VAR"
Now define the variables differently in your .bashrc
:
LOCAL_VAR="local_value"
export GLOBAL_VAR="global_value"
After saving and sourcing your .bashrc
, run the script:
$ source ~/.bashrc
$ chmod +x test.sh
$ ./test.sh
The output will demonstrate that only the exported variable is accessible.
For most use cases where you want the variable to persist and be available:
- Always use
export
for environment variables - Use uppercase names by convention (e.g.,
JAVA_HOME
) - Put exports near the top of your
.bashrc
file - Add comments explaining the variable's purpose
Here are some typical examples you might include in your .bashrc
:
# Java development
export JAVA_HOME=/usr/lib/jvm/java-11-openjdk-amd64
export PATH=$JAVA_HOME/bin:$PATH
# Python virtualenv
export WORKON_HOME=$HOME/.virtualenvs
export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3
# Custom application settings
export APP_CONFIG_PATH=$HOME/.config/myapp
If your variables aren't working as expected:
- Make sure to
source ~/.bashrc
after making changes - Check for typos in variable names
- Verify file permissions (
chmod 644 ~/.bashrc
) - Look for conflicting definitions later in the file