Desktop

桌面環境的 $PATH 環境變數與 shell 中的環境變數是否不同?

  • July 1, 2018

我試圖找到.desktopqtcreator 的文件,所以我寫了這個:

$ for p in ${XDG_DATA_DIRS//:/ }; do
   grep -rni 'qtcreator' $p;
done

這是最相關的行:

/usr/share/app-install/desktop/qtcreator-plugin-ubuntu:ubuntusdk.desktop:2:X-AppInstall-Package=qtcreator-plugin-ubuntu
/usr/share/app-install/desktop/qtcreator-plugin-ubuntu:ubuntusdk.desktop:6:Exec=qtcreator %F
/usr/share/app-install/desktop/qtcreator-plugin-ubuntu:ubuntusdk.desktop:7:Icon=ubuntu-qtcreator
/usr/share/app-install/desktop/qtcreator:qtcreator.desktop:2:X-AppInstall-Package=qtcreator
/usr/share/app-install/desktop/qtcreator:qtcreator.desktop:6:Exec=qtcreator %F
/usr/share/app-install/desktop/qtcreator:qtcreator.desktop:7:Icon=QtProject-qtcreator
/usr/share/app-install/desktop/qhimdtransfer:qhimdtransfer.desktop:12:#Icon=qtcreator_logo_32

我認為這qtcreator:qtcreator.desktop是 QtCreator 的桌麵條目,所以我打開它並找出:

[Desktop Entry]
X-AppInstall-Package=qtcreator
X-AppInstall-Popcon=292
X-AppInstall-Section=universe

Exec=qtcreator %F
Icon=QtProject-qtcreator
Type=Application
Terminal=false
Name=Qt Creator
GenericName=Integrated Development Environment
MimeType=text/x-c++src;text/x-c++hdr;text/x-xsrc;application/x-designer;application/vnd.nokia.qt.qmakeprofile;application/vnd.nokia.xml.qt.resource;
Categories=Qt;Development;IDE;
InitialPreference=9

X-Ubuntu-Gettext-Domain=app-install-data

qtcreator 不在 shell 的變數 $PATH 中。但我仍然可以在 gnome unity 中啟動 QtCreator。

從我注意到的freedesktop-exec-variables

如果未提供完整路徑,則在桌面環境使用的 $PATH 環境變數中查找執行檔。

問題

Is $PATH environment variable of a desktop environment different from that is a shell?

If so, where is the config file containing the $PATH variable for desktop environment?

I install a newer version of QtCreator then launch QtCreator in desktop and find out it references to the newer version without changing the qtcreator:qtcreator.desktop file. I think there is something to with the path of qt?

Desktop environment: gnome

OS: ubuntu16.04

Edit

The actual problem I encountered was not about $ PATH(see my answer blow). Gilles’ answer is actually the answer to `Is $ PATH environment variable of a desktop environment different from that in a shell?’ so I accepted it in case that someone has the same doubt as me.

Is $PATH environment variable of a desktop environment different from that is a shell?

It may be.

The environment of a program does not come from a file, unless the program goes out of its way to read environment variables from a file. Each process inherits the environment of its parent. Reading environment variables from a file is part of the job of the program you run upon login (a shell when logging in in text mode, a session manager or the program that acts as such when logging in in a graphical environment). So normally, if you use your desktop environment and open a shell in a terminal, that shell would have the same environment as the desktop environment.

However, in practice, this may not be the case, because there’s a ton of advice on the web telling people to put environment variable definitions in .bashrc. That’s a bad idea because it means that these environment variables are not available to programs started directly from the GUI, rather than started from a terminal. But if you’ve done that, you may find that you have different environment variables in a terminal compared to what you have in the desktop environment and in programs not launched from a terminal.

Putting environment variable definitions in ~/.profile works on most systems. I think it works for Gnome on Ubuntu 16.04 as long as you don’t activate Wayland (which breaks a lot of things, including login settings, and doesn’t always offer a way to recover).

In any case, the usual symptoms of putting environment variables in the wrong place is that environment variables are present in a terminal, but missing in the desktop environment. For it to be the other way round, you would have to have something unusual in your shell startup files.

qtcreator is not in the variable $PATH of the shell.

I very much doubt it, if it’s installed. Ubuntu ships qtcreator in /usr/bin which is where most programs are located.

But note that $PATH has nothing to do with the location of the .desktop file. $PATH is for application programs, not for .desktop files. If you’re looking for the .desktop file for qtcreator, and you have the qtcreator package installed, you can just search for the file:

$ locate qtcreator.desktop
/usr/share/applications/qtcreator.desktop

/usr/share/applications is the standard location for .desktop files.

If you’ve installed your own version of QtCreator in addition to the one in Ubuntu, then you should ensure that qtcreator is in the command search path, i.e. $PATH. If you installed it system-wide, you should have it available as /usr/local/bin/qtcreator, which is on the command search path before /usr/bin, so you’ll get your version in preference to the system one. If you installed it on your account, create a symbolic link from wherever the qtcreator executable is to ~/bin/qtcreator, i.e. to the directory bin under your home directory (if this directory doesn’t exist, create it). Ubuntu’s default .profile adds ~/bin to the front of $PATH.

引用自:https://unix.stackexchange.com/questions/452842