0

I need to get the value of a environment variable in linux(UBUNTU). For this purpose I am using "qgetenv" function with the environment variable name. I can confirm that value of environment variable is set because when I do echo $VARIABLE_NAME, it is perfectly printing the value of the variable. This snippet of the code I am using:

  QByteArray root  = qgetenv("PAPARAZZI_HOME");
  if (root.data()==NULL) {
    cerr << "PAPARAZZI_HOME is not defined" << endl;
    exit(0);
  }
  string pprzRoot = string(root.data());

and echo $PAPARAZZI_HOME prints following string:
"/home/manish/paprazzi-git/paparazzi/".
I am invoking my program using
sudo "Path to Application Dir"/"Application Name".

Answer: "sudo" removes most of the user's environment variables. Therefore, if one wants to preserve the environment variables along with sudo, sudo -E must be used. See "man sudo" for more detail.

Manish
  • 513
  • 1
  • 8
  • 23

1 Answers1

1

Having

$ echo $PAPARAZZI_HOME

print the expected value doesn't verify that $PAPARAZZI_HOME is an environment variable. It could be an unexported shell variable.

If you set it by typing this at your shell prompt:

$ PAPARAZZI_HOME=/home/manish/paprazzi-git/paparazzi/

then it's not an environment variable. Try this:

$ export PAPARAZZI_HOME=/home/manish/paprazzi-git/paparazzi/

and run your program again. or, if you've already assigned a value to it:

$ export PAPARAZZI_HOME

Some older shells, like the original sh, don't support setting and exporting a variable in one command. If you're using such a shell, you'll need to use two commands to set an environment variable:

$ PAPARAZZI_HOME=/home/manish/paprazzi-git/paparazzi/
$ export PAPARAZZI_HOME

If you're using csh or tcsh, the syntax is different. To set a (non-environment) shell variable:

% set PAPARAZZI_HOME = /home/manish/paprazzi-git/paparazzi/

To set an environment variable:

% setenv PAPARAZZI_HOME /home/manish/paprazzi-git/paparazzi/

In csh and tcsh, shell variables and environment variables are not as closely tied as they are in sh, ksh, bash, zsh, et al; there's no direct way (like the export command) to change an existing shell variable to an environment variable. And you can have a shell variable and an environment variable with the same name, in which case $PAPARAZZI_HOME would expand to the value of the shell variable. If you're using bash, you can safely ignore this paragraph.

Another possibility is that your program is being launched in such a way that it doesn't inherit your shell's environment. If you launch the program from the IDE or from some other GUI, it won't inherit environment variables you set in your shell -- unless you launched the IDE or GUI from that shell, and after setting the environment variable. Since you're on Ubuntu, try launching your application by typing its full path at your shell prompt.

Oh, and you probably don't need the trailing /.

** UPDATE :**

After a lengthy discussion in chat, it turns out that the OP is invoking his program using sudo, because it needs root privileges.

sudo, by default, removes most environment variables.

You can ask it to preserve your environment by using sudo -E. man sudo for details.

It's not clear that sudo -E is the best solution; it may have security implications.

If the program only needs that one environment variable, then this:

$ sudo env PAPARAZZI_HOME=/home/manish/paprazzi-git/paparazzi /path/to/the/program

Keith Thompson
  • 254,901
  • 44
  • 429
  • 631
  • I actually set the variable using export and i have also included the line "export PAPARAZZI_HOME=/home/manish/paprazzi-git/paparazzi/" in my .bashrc file. Therefore it is a permanent value. – Manish Aug 20 '13 at 00:02
  • 1
    @Manish: Just to be sure, try `env | grep PAPARAZZI_HOME` – Keith Thompson Aug 20 '13 at 00:04
  • @Manish: Also, try with an environment variable that you know is set, like `$HOME` or `$PATH`. And be sure that you're invoking your program in such a way that it inherits `$PAPARAZZI_HOME` after you've set it. If you're invoking it from a GUI, such as from Qt Creator, it won't inherit environment variables you set in a shell -- unless you launch Qt Creator from that shell. I've updated my answer. – Keith Thompson Aug 20 '13 at 00:12
  • U r right PATH and HOME are working correctly with qgetenv but not with $PAPARAZZI_HOME. What does it mean inheriting $PAPARAZZI_HOME? I am not using Qt Creator. I am using Emacs as my editor, and I use qmake and make for building the program – Manish Aug 20 '13 at 00:17
  • @Manish: How exactly are you launching your program? Did you try launching it by typing its full path at your shell prompt? – Keith Thompson Aug 20 '13 at 00:26
  • After setting the environment variable according to the answer provided in this post "http://stackoverflow.com/questions/2655641/set-environment-variable-in-ubuntu"(means using "/etc/environment" file) it started working. But I am really clueless about what is happening behind the scene or I am having an incorrect notion about environment variable in liunux? – Manish Aug 20 '13 at 00:32
  • You still haven't explained how you're launching your program or whether launching the program from your shell worked. Environment variables are inherited only by child processes. – Keith Thompson Aug 20 '13 at 00:43
  • I launched my program from the directory where the executable was being built. – Manish Aug 20 '13 at 00:47
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/35806/discussion-between-keith-thompson-and-manish) – Keith Thompson Aug 20 '13 at 00:49