How to make environment variables persistent?

0237

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:

  1. Open the .bashrc or .bash_profile file:
    You can use a text editor like nano or vim. For example:

    nano ~/.bashrc

    or

    nano ~/.bash_profile
  2. Add the Environment Variable:
    At the end of the file, add a line to export your variable. For example:

    export MY_VARIABLE="my_value"
  3. Save and Exit:
    If you are using nano, press CTRL + X, then Y, and Enter to save and exit.

  4. Apply the Changes:
    To apply the changes without restarting the terminal, run:

    source ~/.bashrc

    or

    source ~/.bash_profile

For Zsh Users:

  1. Open the .zshrc file:

    nano ~/.zshrc
  2. Add the Environment Variable:
    Add the export line as shown above:

    export MY_VARIABLE="my_value"
  3. Save and Exit:
    Save the changes as described earlier.

  4. 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.

0 Comments

no data
Be the first to share your comment!