How to Set Environment Variables in Linux

Setting environment variables in Linux very easy. All we have to do is, export the variable using export command.


export FRUIT

Set Environment Variables in Linux

It is also possible to set and export in one line.

export FRUIT=apple

Set Persistent Environment Variables in Linux

The problem is when you set Environment variables from the command line, they are not permanent and cannot be accessed from a new shell. Variables will be lost after the system reboots.

We can make environment variables Persistent by adding it to the either ∼/.bashrc or /etc/profile.

∼/.bashrc file is located in the user home directory, so it affects only a single user only. Use the /etc/profile, If you want to set environment variables for all users in the system.

Display environment variables in Linux

Use either printenv or env command to display all environment variables in your system.

Display environment variables in Linux using either env or printenv command

Use the grep command to filter the result by keyword.

Filter Environment Variables in Linux

What Are Environment Variables in Linux?

Linux environment variables are accessible from the shell that create them, as well as sub shells and child processes of that shell. When you create a variable in Linux, it is only available to the parent shell. Once you export the variable, it becomes an environment variable and available for the all child process of that shell.