How do I create a new environment variable?

0162

To create a new environment variable, you can use the export command in your terminal. Here’s how to do it:

Temporary Environment Variable

This variable will only exist for the duration of the current shell session.

  1. Open your terminal.

  2. Use the export command to create the variable:

    export MY_VARIABLE="my_value"
  3. You can verify that it has been created by using:

    echo $MY_VARIABLE

Permanent Environment Variable

To make the variable persistent across sessions, you need to add it to your shell's configuration file.

For Bash:

  1. Open the .bashrc file in your home directory:
    nano ~/.bashrc
  2. Add the following line at the end of the file:
    export MY_VARIABLE="my_value"
  3. Save and exit the editor (in Nano, press CTRL + X, then Y, and Enter).
  4. To apply the changes, run:
    source ~/.bashrc

For Zsh:

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

System-wide Environment Variable

To set an environment variable that is available to all users:

  1. Open or create a file in /etc/profile.d/:
    sudo nano /etc/profile.d/my_variables.sh
  2. Add the following line:
    export MY_VARIABLE="my_value"
  3. Save and exit the editor.

After following these steps, the new environment variable will be created and available in your shell sessions.

0 Comments

no data
Be the first to share your comment!