0

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

/path/to/dir

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.

1 Answer 1

2

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).

3
  • Neither of the two answers given worked for me.
    – Jake
    Apr 8, 2012 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, 2012 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, 2012 at 12:01

You must log in to answer this question.

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