Making Environment Variables Persistent
So far, we've been setting environment variables that only last for the current terminal session. Once you close your terminal or log out, these variables are lost. In this step, you'll learn how to make environment variables persistent across sessions.
Storing Environment Variables in Configuration Files
There are several files where you can set environment variables to make them persistent:
~/.bashrc
or ~/.zshrc
: For variables specific to a user
/etc/environment
: For system-wide variables
/etc/profile
or files in /etc/profile.d/
: For system-wide variables loaded at login
Let's add some environment variables to your user's shell configuration file. Since this lab environment uses ZSH, we'll edit the ~/.zshrc
file:
nano ~/.zshrc
Scroll to the bottom of the file and add the following lines:
## Custom environment variables
export EDITOR="nano"
export CUSTOM_PATH="$HOME/bin"
export GREETING="Hello from .zshrc!"
Save the file (press Ctrl+O, then Enter) and exit nano (press Ctrl+X).
To apply these changes to your current session, you need to source the file:
source ~/.zshrc
Now, check if the variables are set:
echo $EDITOR
echo $CUSTOM_PATH
echo $GREETING
You should see the values you set in the .zshrc
file.
These environment variables will now be available every time you start a new shell session.
Creating a Custom Environment Variables File
A good practice for managing environment variables is to create a separate file just for them, especially for project-specific variables. This makes it easier to manage and share configurations.
Let's create a file called .env
in your project directory:
cd ~/project
nano .env
Add the following content:
## Project environment variables
export PROJECT_NAME="Linux Environment Lab"
export PROJECT_VERSION="1.0.0"
export DEBUG_MODE="true"
Save the file (press Ctrl+O, then Enter) and exit nano (press Ctrl+X).
To load these variables into your current session, source the file:
source ~/project/.env
Now check if the variables are set:
echo $PROJECT_NAME
echo $PROJECT_VERSION
echo $DEBUG_MODE
You should see the values from the .env
file.
Creating a Script to Load Environment Variables
Finally, let's create a script that loads environment variables from a file. This is a common pattern in development environments:
cd ~/project
nano load_env.sh
Add the following content:
#!/bin/bash
## Script to load environment variables from a .env file
ENV_FILE=".env"
if [[ -f "$ENV_FILE" ]]; then
echo "Loading environment variables from $ENV_FILE"
## Read each line from the .env file
while IFS= read -r line || [[ -n "$line" ]]; do
## Skip comments and empty lines
if [[ $line =~ ^## ]] || [[ -z $line ]]; then
continue
fi
## Export the variable if it starts with "export "
if [[ $line == export* ]]; then
## Remove the "export " prefix and export the variable
eval "${line}"
echo "Exported: ${line#export }"
fi
done < "$ENV_FILE"
echo "Environment variables loaded successfully"
else
echo "Error: $ENV_FILE file not found"
exit 1
fi
Save the file (press Ctrl+O, then Enter) and exit nano (press Ctrl+X).
Make the script executable:
chmod +x ~/project/load_env.sh
Now, unset the variables we previously set and then run the script to load them again:
unset PROJECT_NAME PROJECT_VERSION DEBUG_MODE
echo "PROJECT_NAME: $PROJECT_NAME"
## Now load the variables using the script
~/project/load_env.sh
## Check if the variables are now set
echo "PROJECT_NAME: $PROJECT_NAME"
The script reads the .env
file and exports each variable defined with the export
keyword.
This approach is commonly used in development environments to manage project-specific environment variables.