How To Set Environment Variables For Linux, Windows, BSD and MacOSX

RSS
FACEBOOK
FACEBOOK
PINTEREST
PINTEREST
LINKEDIN
INSTAGRAM

Environment variables are very useful for setting some data in the operationg system environment. They are like key-value pairs where we can set a key a specific value which can be used by all applications in the current operating system. Environment variables are used to set library path, Java path, Java version, current username, current connection type etc.

List All Environment Variables For Linux and MacOSX

We can list Linux operating system environment variables in different ways. The most popular way to list all environment variables is using env command.

List All Environment Variables For Linux
List All Environment Variables For Linux

We can see that environment variables like USERNAME, USER, SHELL, LANG etc. are provided

List Environment Variable For Linux and MacOSX

If we can to just print and list specific environment variable we need to use grep command like below with the env command. In this example, we will list environment variables where their key or value contains the USER term.

List Environment Variable For Linux
List Environment Variable For Linux

We can see that USERNAME and USER variable keys are filtered and printed.

We can also use the echo command by providing the variable name which is USERNAME in this example.

List Environment Variable For Linux
List Environment Variable For Linux

Set Environment Variable For Linux and MacOSX

Now, this is the most interesting part in setting an environment variable key and value. We can set environment variables permanently or temporarily.

Set Permanently

Setting permanently will store the variable during reboots and restarts. It will not be removed after a system restart. We will use bash shell .profile file to set environment variable permanently. Add the following lines to the user’s profile file like below.

LEARN MORE  Linux Bash How to Define, Use Shell Variables

Run following command which will add key and value to the users .profile file.

Set Permanently
Set Permanently

For zsh shell we will add to .zprofile file like below.

For ksh or KornShell we will add to .profile file

For bourne shell we will add to .profile file

For csh shell we will add to .login file

Set Temporarily

If we want to set an environment variable temporarily for just current session for all users we can use export and set commands. set will add the environment variable and export will add variable to all other users environment.

Set Temporarily
Set Temporarily

List All Environment Variables For Windows

We can use the set command in order to print currently used environment variables from the MS-DOS command line interface.

List All Environment Variables For Windows
List All Environment Variables For Windows

We can see that popular environment variables like PATH, PROMPT,OS and their values are printed in this example.

List Environment Variable For Windows

If we want to just print single environment variable value we can use echo command by providing the environment variable key or name. In this example, we will print the value of USERNAME.

List Environment Variable For Windows
List Environment Variable For Windows

Set Environment Variable For Windows

We can set an environment variable with the set command from both MS-DOS and Powershell. We will also provide the key or variable name and the value. In this example, we will set the variable name WEBSITE to the value POFTUT.COM

Set Environment Variable For Windows
Set Environment Variable For Windows

We can also use GUI to list and set the environment variable. We will open Environment Variables pane Computer->System Properties->Performance Settings->Environment Variables

As we can see that we can add User specific or System variables from this pane. Also, the user and system variables are printed in this pane too.

LEARN MORE  C - Union
Set Environment Variable For Windows
Set Environment Variable For Windows

When we click to the Newin the system variables part we will see the following screen where we can set Variable name or Key and Variable value.

Set Environment Variable For Windows
Set Environment Variable For Windows

We can see that the system variable WEBSITE is added to the system variables list.

List Environment Variable For Windows
List Environment Variable For Windows

RSS
FACEBOOK
FACEBOOK
PINTEREST
PINTEREST
LINKEDIN
INSTAGRAM