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.
$ env

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.
$ env | grep USER

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.
$ echo $USERNAME

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.
WEBSITE="POFTUT.COM"
Run following command which will add key and value to the users .profile
file.
$ echo "export WEBSITE=POFTUT.COM" >> ~/.profile

For zsh shell we will add to .zprofile
file like below.
$ echo "export WEBSITE=POFTUT.COM" >> ~/.zprofile
For ksh or KornShell we will add to .profile
file
$ echo "export WEBSITE=POFTUT.COM" >> ~/.profile
For bourne shell we will add to .profile
file
$ echo "export WEBSITE=POFTUT.COM" >> ~/.profile
For csh shell we will add to .login
file
$ echo "export WEBSITE=POFTUT.COM" >> ~/.login
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.
$ export set WEBSITE="POFTUT.COM"

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.
> set

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
.
>echo %USERNAME%

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 WEBSITE="POFTUT.COM"

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.

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

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