Environment variables are a set of dynamic named values in Linux, used by applications launched in shell. It allow you to customize how the system works and the behavior of the applications. For example, the environment variable can store information about the default browser or the path to executable files.

This article discuss about working with environment variable. We will dive into

• Setting variable
• Unsetting variable
• Exporting variable

## Environment and Shell Variable

Environment variables are variables that are available system-wide and are inherited by all child processes and shells. Shell variables are variables that apply only to the current shell instance.

Commands to work with environment variables in Linux:

• env – The command allows you to run another program in a custom environment without modifying the current one. When used without an argument it will print a list of the current environment variables.
• printenv – Prints all or the specified environment variables.
• set – Sets or unsets shell variables. When used without an argument it will print a list of all variables including environment and shell variables, and shell functions.
• unset – Deletes shell and environment variables.
• export – Sets environment variables.

## List Environment Variable

printenv prints a list of all environment variables, one variable per line. If the name of the variable is passed as an argument to the command, only the value of that variable is displayed.

For example, to display the value of the HOME environment variable you would run:

printenv HOME

# The output will print the path of the currently logged in user:
/home/rextester_user339

Most common environment variables are:

• USER – Current logged in user.
• HOME – Home directory of the current user.
• EDITOR – Default file editor to be used. This is the editor that will be used when you type edit in your terminal.
• SHELL – Path of the current user’s shell, such as bash or zsh.
• LOGNAME – The name of the current user.
• PATH – A list of directories to be searched when executing commands. When you run a command the system will search those directories in this order and use the first found executable.
• LANG – The current locales settings.
• TERM – The current terminal emulation.

The printenv and env commands print only the environment variables. If you want to get a list of all variables, including environment, shell and variables, and shell functions you can use the set command.

## Setting Environment Variable

Following example shows how to create a new shell variable MY_VAR.

MY_VAR='Linux'