How to make a variable permanent?

0137

To make a shell variable permanent, you need to add it to a shell configuration file that is executed whenever a new shell session starts. Here are the steps to do this:

  1. Open the Shell Configuration File: Depending on the shell you are using, you can add the variable to one of the following files:

    • For bash, use ~/.bashrc or ~/.bash_profile.
    • For zsh, use ~/.zshrc.

    You can open the file using a text editor, for example:

    nano ~/.bashrc
    
  2. Add the Variable: Add the variable definition at the end of the file. For example:

    export MY_VAR="Hello, Permanent Variable"
    
  3. Save and Exit: Save the changes and exit the text editor.

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

    source ~/.bashrc
    

    or

    source ~/.bash_profile
    
  5. Verify the Variable: You can verify that the variable is set by running:

    echo $MY_VAR
    

    Output:

    Hello, Permanent Variable
    

Now, the variable will be available in all new terminal sessions.

0 Comments

no data
Be the first to share your comment!