To make environment variables persistent across shell sessions in Linux, you need to define them in a shell configuration file. Here’s how to do it:
For Bash Users:
Open the
.bashrcor.bash_profilefile: You can use a text editor likenanoorvim. For example:nano ~/.bashrcor
nano ~/.bash_profileAdd the Environment Variable: At the end of the file, add a line to export your variable. For example:
export MY_VARIABLE="my_value"Save and Exit: If you are using
nano, pressCTRL + X, thenY, andEnterto save and exit.Apply the Changes: To apply the changes without restarting the terminal, run:
source ~/.bashrcor
source ~/.bash_profile
For Zsh Users:
Open the
.zshrcfile:nano ~/.zshrcAdd the Environment Variable: Add the export line as shown above:
export MY_VARIABLE="my_value"Save and Exit: Save the changes as described earlier.
Apply the Changes: Run:
source ~/.zshrc
Verification:
To verify that the environment variable is persistent, you can open a new terminal session and run:
echo $MY_VARIABLE
If it outputs the value you set, the variable is now persistent across sessions.
