6

In my ~/.bashrc and ~/.profile I have some variables set to some filepaths:

export MY_VAR1="/path/to/somewhere"

In my ~/bin/ (which is added to my PATH) I have a few scripts that rely on those variables being set.

If I run those commands from the terminal, they work as expected.

However, if I try to double click on a script in Nautilus and run it, it runs as if those variables were not set.

Same thing happens if I add one of the scripts to Gnome's "Startup Applications" using the GUI utility or if I add them to the Gnome menu using the "Main Menu" GUI utility.

I would've thought that since I included these variables in my ~/.profile, which is run at login that gnome and nautilus and processes spawned by them would have access to those variables.

Both Gnome and Nautilus are run under my login user. All my scripts in ~/bin have the bash shebang at the top.

I am new to bash scripting, sorry if I am missing something obvious. I am using Debian Wheezy and Gnome3.

7

That is because a starting X session never reads your ~/.bashrc and ~/.profile. Usually, the desktop manager is started as root or its own user from an init script. The resulting process usually has the environment of the init process when it hits the desktop manager starting script. (I will not talk about less established init implementations at this point.)

After login it spawns a child process, which inherits just that environment. The child process drops privileges to your uid and runs /etc/X11/Xsession, which usually simply runs all the scripts in /etc/X11/Xsession.d. These scripts usually either set environment variables, or, specifically modify the invocation of the desktop environment.

In that directory you may find a file like 40x11-common_xsessionrc on Debian systems, which sources your ~/.xsessionrc. The last file in /etc/X11/Xsession.d will start your desktop environment. All your GUI processes will be spawned from this process, thus inherit the environment set up through /etc/X11/Xsession.d and consequently your ~/.xsessionrc.

~/.xsessionrc is a script file, which may source ~/.bashrc but that's really bad style, because then your DE sources it, the terminal process inherits that environment and then bash sources ~/.bashrc again, which may have unwanted side effects.

However, it is totally acceptible to have a ~/.myenvironmentvariables, which contains stricly (environment) variable definitions and is sourced by ~/.bashrc and ~/.xsessionrc. If you do that, all your processes will inherit the variables specified in ~/.myenvironmentvariables, especially the shell scripts you double click in Nautilus.

  • Worked like a charm, thank you for your thorough explanation! :) – Felix Mc Nov 26 '14 at 22:55
  • Starting an X session from a display manager (i.e. logging in in graphics mode) does sometimes read ~/.profile. It depends on the display manager, on the session type and on the distribution. – Gilles Nov 26 '14 at 23:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.