How to set environment variables?

0138

You can set environment variables in various ways depending on the operating system and the shell you are using. Here are some common methods:

In Unix/Linux (Bash Shell)

  1. Temporary Environment Variable (only for the current session):

    export VARIABLE_NAME="value"
  2. Persistent Environment Variable (across sessions):

    • Open your .bashrc or .bash_profile file:
      nano ~/.bashrc
    • Add the following line at the end of the file:
      export VARIABLE_NAME="value"
    • Save and exit the editor, then run:
      source ~/.bashrc

In Unix/Linux (Zsh Shell)

  1. Temporary Environment Variable:

    export VARIABLE_NAME="value"
  2. Persistent Environment Variable:

    • Open your .zshrc file:
      nano ~/.zshrc
    • Add the following line:
      export VARIABLE_NAME="value"
    • Save and exit, then run:
      source ~/.zshrc

In Windows (Command Prompt)

  1. Temporary Environment Variable:

    set VARIABLE_NAME=value
  2. Persistent Environment Variable:

    setx VARIABLE_NAME "value"

In Windows (PowerShell)

  1. Temporary Environment Variable:

    $env:VARIABLE_NAME="value"
  2. Persistent Environment Variable:

    [System.Environment]::SetEnvironmentVariable("VARIABLE_NAME", "value", "User")

Example

For example, to set a variable named MY_VAR with the value HelloWorld in a Bash shell, you would do:

export MY_VAR="HelloWorld"

To make it persistent, add the same line to your .bashrc or .bash_profile file and source it.

0 Comments

no data
Be the first to share your comment!