Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I am writing a bash script that needs to know which desktop environment (XFCE, Unity, KDE, LXDE, Mate, Cinnamon, GNOME2, GNOME3,... ) is running.

How can I obtain that information?

share|improve this question
    
that is hard because even if you are in a GNOME session and just started one KDE program like kwrite all the KDE infrastructure like KDE daemon and kdeinit will be running. –  Thorsten Staerk Feb 23 at 13:37
2  
You pretty much can't, not reliably. Why do you need to know? This looks like an XY problem. –  Gilles Feb 23 at 23:40
    
@Gilles Good question. I am writing a speciality script for xplanet and would like to automatically refresh the desktop background with commands specific to the desktop environment. If you like to post an answer to that problem, please, follow the link. –  Serge Stroobandt Feb 24 at 8:46
add comment

7 Answers

up vote 2 down vote accepted

The main problem with checking the DESKTOP_SESSION is that it is set by the display manager rather than the desktop session and is subject to inconsistencies. For lightdm on Debian, the values come from the names of files under /usr/share/xsessions/. DESKTOP_SESSION reflects the desktop environment if a specific selection is made at log in, however the lightdm-xsession is always used the default session.

GDMSESSION is another option, but seems to have a similar predicament (it is the same value as DESKTOP_SESSION for me).

XDG_CURRENT_DESKTOP looks like a good choice, however it is currently not in the XDG standard and thus not always implemented. See here for a discussion of this. This answer shows its values for different distros/desktops, I can also confirm it is currently not available for me on XFCE.

The reasonable fallback for XDG_CURRENT_DESKTOP not existing would be to try XDG_DATA_DIRS. Provided the data files for the desktop environment are installed in a directory bearing its name, this approach should work. This will hopefully be the case for all distros/desktops!

The following (with GNU grep) tests for XFCE, KDE and Gnome:

echo "$XDG_DATA_DIRS" | grep -Eo 'xfce|kde|gnome'

POSIX compatible:

echo "$XDG_DATA_DIRS" | sed 's/.*\(xfce\|kde\|gnome\).*/\1/'

To combine with checking XDG_CURRENT_DESKTOP:

if [ "$XDG_CURRENT_DESKTOP" = "" ]
then
  desktop=$(echo "$XDG_DATA_DIRS" | sed 's/.*\(xfce\|kde\|gnome\).*/\1/')
else
  desktop=$XDG_CURRENT_DESKTOP
fi

desktop=${desktop,,}  # convert to lower case
echo "$desktop"
share|improve this answer
    
I like this. According to this overview, there will only be a problem with Cinnamon. However, this might eventually be resolved by opening a bug report with Linux Mint. –  Serge Stroobandt Feb 24 at 17:05
    
@on4aa MATE is not on the list, it is likely to have the same issue. apt-file is also a good tool to see where the various desktops install to. –  Graeme Feb 24 at 17:38
    
This solution is still leaning heavily towards Debian based distros. None of the methods discussed work on any of the Red Hat based distros I have access to (Fedora, CentOS, etc.). –  slm Feb 26 at 16:34
    
@slm interesting, does XDG_DATA_DIRS not exist or does it just not contain anything useful? –  Graeme Feb 26 at 16:45
1  
@slm very true, I think the OP is probably better trying to do what he wants to do for each desktop in a || style. Still this question has been asked on other SE sites, I think we have the best set of answers. –  Graeme Feb 26 at 17:02
show 4 more comments

That probably depends on the situation. If you know which display manager is used then it may be that this one puts this information in a environment variable.

If that is not the case then I guess you have to check for every DE you want to be able to identify. All of them should introduce their own environment variables.

share|improve this answer
    
I am wondering whether echo ${DESKTOP_SESSION:0:1} would do the trick? As far as I could test, it returns u for Unity and x for XFCE. Hopefully some folk will chime in for KDE and other desktops. –  Serge Stroobandt Feb 23 at 16:16
1  
@on4aa It's echo ${DESKTOP_SESSION} kde-plasma-safe for my KDE. Whyever "safe"... –  Hauke Laging Feb 23 at 16:23
    
Are there some Linux Mint Mate/Cinnamon users here? E17, LXDE, etc. are also welcome... –  Serge Stroobandt Feb 23 at 16:32
add comment

Method #1 - $DESKTOP_SESSION

I think you can find out by interrogating the environment variable $DESKTOP_SESSION. I'm not entirely positive how widely supported this is but in my limited testing it appears to be available on Fedora & Ubuntu.

$ echo $DESKTOP_SESSION
gnome

Method #2 - wmctrl

There is also this method that makes use of wmctrl.

$ wmctrl  -m
Name: GNOME Shell
Class: N/A
PID: N/A
Window manager's "showing the desktop" mode: N/A

References

share|improve this answer
1  
@on4aa - $GDMSESSION makes me nervous since it's likely only geared towards DE that are either using GDM or GNOME based DE. GDM = GNOME Display Manager. –  slm Feb 23 at 17:04
1  
@on4aa DESKTOP_SESSION on xfce. –  Graeme Feb 23 at 17:38
1  
on xfce, my DESKTOP_SESSION is default (mint 15) –  glenn jackman Feb 23 at 18:34
1  
Debian showed default for this too. A default install of GNOME 3. –  slm Feb 23 at 18:44
1  
$DESKTOP_SESSION shows default for KDE under Fedora 20. –  Matthew Cline Feb 23 at 22:51
show 6 more comments

If the environmental variable XDG_CURRENT_DESKTOP is available, it should tell you.

# echo $XDG_CURRENT_DESKTOP
KDE
share|improve this answer
1  
Not a surprise but just so other's are aware that pass by this Q&A: doesn't exist in in GNOME DE. –  slm Feb 23 at 23:37
    
Indeed, with emphasis on "if available". See also this Askubuntu answer. –  Serge Stroobandt Feb 24 at 8:16
add comment

You could look for running Xorg processes. The parent of this should be your display manager. Its descendants should give an indication of what desktop environment is running. On my system, the display manager executes itself (with different parameters). This then spawns x-session-manager which is symlinked to xfce4-session. This may be enough, but all the children of this are related to my desktop environment. Finding them via the process tree should be the best way to exclude elements of other window systems started by various programs (or perhaps deliberately).

My first thought was that it would be be best to look for the window manager associated with your desktop environment, but often a different one can be configured to run (eg xmonad in Gnome) so this is not robust. The best one to look for is probably the one which manages the actual desktop, eg xfdesktop or whatever element of the desktop environment you actally need for your script to work :)

Example

Here is an example using procps-ng (-C and --ppid are not POSIX). It assumes the is only one instance of Xorg.

This is just an example that works for the xfce desktop. A full working example requires an investigation in to the processes that each desktop system uses, just as most of the other solutions require investigation into how environmental variables are set in various other desktop systems.

X=Xorg

search_descendants ()
{
  ps h -o comm --ppid $1 |
    grep -E '^(xfdesktop|another_desktop_process)$' &&
    return 0

  for pid in $(ps h -o pid --ppid $1)
  do
    search_descendants $pid && return 0
  done

  return 1
}

dm_pid=$(ps h -o ppid -C "$X")

case "$(search_descendants $dm_pid)" in
  xfdesktop)
    desktop=xfce
    ;;
  another_desktop_process)
    desktop=another_desktop
    ;;
  *)
    desktop=unknown
    ;;
esac

echo $desktop
share|improve this answer
    
This returns unknown on Ubuntu. –  Serge Stroobandt Feb 24 at 8:41
    
@on4aa, that was not meant to be a complete solution. This was supposed to be an example that works for the xfce desktop. A full working example requires an investigation in to the processes that each desktop system uses. I thought that would have been obvious from reading the code. –  Graeme Feb 24 at 9:39
    
@Greame You are right; I was a bit too quick and overlooked the unfinished another_desktop outcome. –  Serge Stroobandt Feb 24 at 10:33
add comment

You can use this bash script. It can detect desktop environment name and version.

share|improve this answer
add comment

From all the experimenting reported in the numerous comments, I think its my duty as the OP to come up with a consensus answer. (Rest assured, I would be happy to review this answer should contrasting data become available.)

For now, it seems best to take our lead from $XDG_CURRENT_DESKTOP when this variable is defined. If not, $XDG_DATA_DIRS may provide the desired information, more so than the first letter of ${DESKTOP_SESSION:0:1}.

share|improve this answer
    
I have no XDG_CURRENT_DESKTOP. I think the main problem with these is variables in that they are set by the display manager rather than the desktop environment and so there is some inconsistency. When I choose a default session (xfce) with lightdm echo $DESKTOP_SESSION gives lightdm-xsession which is an amalgamation of lightdm and x-session-manager, the process used to start my desktop (symlinked to xfce4-session). I imagine installing with a different default session will just use a different symlink and yield the same DESKTOP_SESSION –  Graeme Feb 24 at 10:14
    
Notably, if I specifically choose XFCE or Gnome from the display manager, DESKTOP_SESSION will actually say xfce or gnome –  Graeme Feb 24 at 10:15
1  
This solution is still leaning heavily towards Debian based distros. None of the methods discussed work on any of the Red Hat based distros I have access to (Fedora, CentOS, etc.). –  slm Feb 26 at 16:35
add comment

Your Answer

 
discard

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.