Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have an environment variable that I've set in ~/.profile with the following line:

export APPDIR=/path/to/dir

When I log in and load up a terminal, I can verify that the variable is set:

$ printenv APPDIR


I'm trying to access this variable from within a Qt application:

QString appdir = getenv("APPDIR");

QTWARNING("dir: |" + appdir + "|");

The warning window that pops up shows me:

dir: ||

What is going on here? Am I misunderstanding about how environment variables work in Ubuntu?

This is with a C++/Qt App on Ubuntu 11.10 x86.

share|improve this question
up vote 2 down vote accepted

If you launch the program from a terminal, the environment variable will be set; by default Linux desktop environments do not run ~/.profile while setting up the GUI, so settings from it are only available in terminals. See How do I set an environment variable in a Unity session? for setting environment variables for the GUI environment (this is not specific to Unity, but works for any X11 session).

share|improve this answer
Neither of the two answers given worked for me. – Jake Apr 8 '12 at 22:59
How are you starting the program? And right, Ubuntu changed its display manager and doesn't use that session config any more :/ unfortunately it looks like the rules are different for different Ubuntu releases which is not helpful. (Some read .profile, some .gnomerc, some want .pam_environment, some use that session file, some require editing files under /usr/share/xsessions, etc. Consistency is not for Ubuntu users, apparently.) – geekosaur Apr 8 '12 at 23:19
I was starting the program with sudo ./appbinary. After your comment I realized that it was loading different environment variables for sudo. I stopped using sudo (turned out the app didn't need it) and that fixed the problem. Thanks :] – Jake Apr 9 '12 at 12:01

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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