To make environment variable exports permanent in a Bash shell, you need to add them to a shell configuration file that is executed whenever a new shell session starts. Here’s how to do it:
Steps to Make Exports Permanent
Choose the Right Configuration File:
- For user-specific settings, you typically use:
~/.bashrc: For interactive non-login shells.~/.bash_profileor~/.profile: For login shells.
- For system-wide settings, you can use:
/etc/profile: For all users./etc/bash.bashrc: For all users in non-login shells.
- For user-specific settings, you typically use:
Open the Configuration File: Use a text editor to open the appropriate file. For example, to edit
~/.bashrc, you can use:nano ~/.bashrcAdd the Export Command: At the end of the file, add your export commands. For example:
export MY_VAR="Hello, World!"Save and Exit: Save the changes and exit the text editor. In
nano, you can do this by pressingCTRL + X, thenY, andEnter.Apply the Changes: To apply the changes immediately without restarting the terminal, run:
source ~/.bashrcor, if you edited
~/.bash_profile:source ~/.bash_profile
Example
Here’s a complete example of making an export permanent:
Open
~/.bashrc:nano ~/.bashrcAdd the export line:
export MY_VAR="Hello, World!"Save and exit, then apply the changes:
source ~/.bashrcVerify the variable is set:
echo $MY_VAR
Summary
- Add export commands to
~/.bashrcor~/.bash_profilefor user-specific variables. - Use
sourceto apply changes immediately. - For system-wide variables, modify files in
/etc/.
If you have any further questions or need clarification, feel free to ask!
