How to set a new environment variable?

088

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

  1. Open your terminal.
  2. Use the following syntax to set a new environment variable:
export VARIABLE_NAME="value"

For example, to set a variable named MY_VAR with the value HelloWorld, you would run:

export MY_VAR="HelloWorld"
  1. To verify that the variable has been set, you can use:
echo $MY_VAR

This should output:

HelloWorld

Making it Persistent

If you want the environment variable to persist across terminal sessions, add the export command to your shell's configuration file (e.g., ~/.bashrc or ~/.zshrc):

  1. Open the configuration file in a text editor:
nano ~/.zshrc  # for zsh users
  1. Add the export command at the end of the file:
export MY_VAR="HelloWorld"
  1. Save and exit the editor.
  2. To apply the changes, run:
source ~/.zshrc

Now, MY_VAR will be available in future terminal sessions.

0 Comments

no data
Be the first to share your comment!