To set an environment variable in Linux, you can use the export
command. This command allows you to set a value for a specified environment variable that will be available to the current shell and any processes spawned by the shell.
For example, to set the FOO
environment variable to bar
, you can use the following command:
export FOO=barSource:www.lautturi.com
To view the value of an environment variable, you can use the echo
command:
echo $FOO
To set multiple environment variables at once, you can use the export
command multiple times:
export FOO=bar export BAZ=qux
To set an environment variable permanently, you can add the export
commands to the ~/.bashrc
file. This file is executed whenever you open a new terminal window, so the environment variables will be set every time you start a new shell.
Keep in mind that the environment variables set with the export
command are only available to the current shell and any processes spawned by the shell. To set environment variables that are available to all processes on the system, you will need to edit the system-wide environment configuration files, such as /etc/environment
or /etc/profile
.
It is also possible to set environment variables for a specific user by adding the export
commands to the user's ~/.bashrc
file. These environment variables will be set every time the user logs in.