1

In Windows, you can use the following window to set the Environment Variables that each process will have once executed:

enter image description here

Can you do the same thing in Linux?

1
  • It depends, on which processes, exactly? Those started from a user's login session? From any user's login session? A non-login session, like one started from ssh somehost somecmd? From a cron job? at jobs? A daemon started by init?
    – ilkkachu
    Apr 29, 2017 at 9:27

3 Answers 3

1

The way to set environment variables for user applications through the user interface works somewhat differently on Windows and on Linux. The Windows interface that you're using changes both the values of variables in the current session, and the values for future sessions.

On Linux, the traditional ways to set environment variables work at login time. There are several ways to do it, but they involve changing files that are only read at login time. So they don't apply to the current session. The main files you can use for this are .profile and .pam_environment, both in your home directory; see What's the best distro/shell-agnostic way to set environment variables? for a more detailed discussion.

To set environment variables for the current session, you have to set them in the process that launches the applications: the GUI shell. Under Windows that's easy because there's no real choice of a GUI shell. (It's technically possible to replace the default one but very few users do it.) Under Linux, there are hundreds of possible GUI shells — the GUI shell is usually the window manager, sometimes a separate component of a desktop environment. There's no standard way to set environment variables in the window manager or desktop environment: each WM/DE has its own way (and some have none, unfortunately).

A WM/DE agnostic of changing environment variables is to edit ~/.profile. A basic syntax (there are other ways) is

export MYVAR='the value of the variable'

Then launch a terminal (which runs a shell), and in that shell type

. ~/.profile

This loads the new values of the environment variables from .profile. After this, if you run a program from that shell, the new variables will apply to that program. But if you run a program directly from a GUI menu, the program will inherit the environment of the program that provides GUI menu, so if you want to change this, you have to use whatever way that program provides (or a companion program as part of the desktop environment).

1

You can set the variable in /etc/environment file. The format must be like:

VARIABLE=value

Be aware that variable expantion do not work in /etc/environment and you need to restart all processes which will use/need this/those variable(s)

4
  • Just to be sure, the environment variables I set in this file will also be available to GUI applications (for example: gedit)?
    – Steve
    Apr 29, 2017 at 3:57
  • AFAIK, /etc/environment, does not exist on Fedora/Red Hat and derivatives so this approach is not portable.
    – fpmurphy
    Apr 29, 2017 at 4:23
  • @Steve, this work for everything, even daemons. So I do not see reason will not work for any login session. fpmurphy, I also did not find reference in RHEL about this file. Apr 29, 2017 at 6:22
  • @RomeoNinov, are you sure about /etc/environment and daemons? AFAIK /etc/environment is used just by pam_env.so, so it only applies for sessions started through PAM. Of course anything could read or source it, but is there such a system where that is done for daemons?
    – ilkkachu
    Apr 29, 2017 at 9:32
1

Environment variables, by their very nature, are available to all processes that are started by the process/shell that set the environment variables.

An environment variable in Unix is a shell variable that is exported using export, e.g.

MYVAR="my value"
export MYVAR

You usually set environment variables in your shell startup file. In the case of bash, this is in ~/.bash_profile, and many other shells uses ~/.profile.

There are system-wide shell initialization scripts under /etc (e.g. /etc/profile) that may be used to customize the environment for all users.

Environment variables may also be set globally for all users in a shell-agnostic manner, but this is done differently depending on your flavour of Unix. Most BSDs may do this by modifying /etc/login.conf while some Linuxes uses /etc/environment or other mechanisms.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .