UNIX: Set Environment Variable

In UNIX-based operating systems (such as Linux and macOS), you can set environment variables using the “export” command. An environment variable is a dynamic value that can be accessed by programs and scripts running in the shell session.

To set an environment variable, follow these steps:

  1. Open a terminal or shell session.
  2. Type “export” followed by the name of the variable you want to set, an equal sign, and the value you want to assign to the variable. For example:
export MYVAR="Hello, world!"

This sets an environment variable named “MYVAR” to the string “Hello, world!”.

  1. To verify that the environment variable has been set, you can use the “echo” command to display its value. For example:
echo $MYVAR

This will display the value of the “MYVAR” environment variable, which in this case is “Hello, world!”.

  1. If you want to unset or remove an environment variable, you can use the “unset” command followed by the name of the variable. For example:
unset MYVAR

This will remove the “MYVAR” environment variable.

Note that the environment variables you set using the “export” command will only be available in the current shell session and its child processes. If you want to set environment variables system-wide or permanently, you can add the “export” commands to a script that is executed during system startup or login, such as the “.bashrc” or “.profile” files.

Leave a Comment