How to set an environment variable?

0160

To set an environment variable in a shell, you can use the export command. Here’s how to do it:

Temporary Environment Variable

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

  1. Open your terminal.

  2. Use the export command to set the variable:

    export MY_VARIABLE="my_value"
  3. You can verify that it has been set 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:
    nano ~/.bashrc
  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 ~/.bashrc

For Zsh:

  1. Open the .zshrc file:
    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 environment variable will be set and available in your shell sessions.

0 Comments

no data
Be the first to share your comment!