How to make variables persistent?

0116

To make shell variables persistent across terminal sessions, you can define them in your shell's configuration file. Here’s how to do it for different shells:

For Bash:

  1. Open the .bashrc file in your home directory:
    nano ~/.bashrc
  2. Add your variable at the end of the file:
    export MY_VARIABLE="my_value"
  3. Save the file and exit the editor.
  4. To apply the changes, run:
    source ~/.bashrc

For Zsh:

  1. Open the .zshrc file in your home directory:
    nano ~/.zshrc
  2. Add your variable at the end of the file:
    export MY_VARIABLE="my_value"
  3. Save the file and exit the editor.
  4. To apply the changes, run:
    source ~/.zshrc

For System-wide Variables:

  1. Edit the /etc/profile file or create a new file in /etc/profile.d/:
    sudo nano /etc/profile.d/my_variables.sh
  2. Add your variable:
    export MY_VARIABLE="my_value"
  3. Save the file and exit the editor.
  4. The changes will take effect for all users after a restart or by sourcing the profile.

By following these steps, your variables will be available in new terminal sessions.

0 Comments

no data
Be the first to share your comment!