How to detect the desktop environment in a bash script?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP

24

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

  • 1

    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 ’14 at 13:37

  • 3

    You pretty much can’t, not reliably. Why do you need to know? This looks like an XY problem.

    – Gilles
    Feb 23 ’14 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 ’14 at 8:46

24

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

  • 1

    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 ’14 at 13:37

  • 3

    You pretty much can’t, not reliably. Why do you need to know? This looks like an XY problem.

    – Gilles
    Feb 23 ’14 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 ’14 at 8:46

24

24

24

3

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

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?

bash desktop-environment bash-script

share|improve this question

share|improve this question

share|improve this question

share|improve this question

asked Feb 23 ’14 at 13:30

Serge StroobandtSerge Stroobandt

81321325

81321325

  • 1

    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 ’14 at 13:37

  • 3

    You pretty much can’t, not reliably. Why do you need to know? This looks like an XY problem.

    – Gilles
    Feb 23 ’14 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 ’14 at 8:46

  • 1

    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 ’14 at 13:37

  • 3

    You pretty much can’t, not reliably. Why do you need to know? This looks like an XY problem.

    – Gilles
    Feb 23 ’14 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 ’14 at 8:46

1

1

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 ’14 at 13:37

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 ’14 at 13:37

3

3

You pretty much can’t, not reliably. Why do you need to know? This looks like an XY problem.

– Gilles
Feb 23 ’14 at 23:40

You pretty much can’t, not reliably. Why do you need to know? This looks like an XY problem.

– Gilles
Feb 23 ’14 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 ’14 at 8:46

@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 ’14 at 8:46

8 Answers
8

active

oldest

votes

11

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 ’14 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 ’14 at 17:38

  • 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 ’14 at 16:34

  • @slm interesting, does XDG_DATA_DIRS not exist or does it just not contain anything useful?

    – Graeme
    Feb 26 ’14 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 ’14 at 17:02

10

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

Another choice is the $XDG_SESSION_DESKTOP variable.

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

  • How to determine which window manager is running
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 ’14 at 17:04

  • 1

    @on4aa DESKTOP_SESSION on xfce.

    – Graeme
    Feb 23 ’14 at 17:38

  • 1

    on xfce, my DESKTOP_SESSION is default (mint 15)

    – glenn jackman
    Feb 23 ’14 at 18:34

  • 1

    Debian showed default for this too. A default install of GNOME 3.

    – slm
    Feb 23 ’14 at 18:44

  • 1

    $DESKTOP_SESSION shows default for KDE under Fedora 20.

    – Matthew Cline
    Feb 23 ’14 at 22:51

5

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

#!/bin/bash

function detect_gnome()
{
    ps -e | grep -E '^.* gnome-session$' > /dev/null
    if [ $? -ne 0 ];
    then
    return 0
    fi
    VERSION=`gnome-session --version | awk '{print $2}'`
    DESKTOP="GNOME"
    return 1
}

function detect_kde()
{
    ps -e | grep -E '^.* kded4$' > /dev/null
    if [ $? -ne 0 ];
    then
        return 0
    else    
        VERSION=`kded4 --version | grep -m 1 'KDE' | awk -F ':' '{print $2}' | awk '{print $1}'`
        DESKTOP="KDE"
        return 1
    fi
}

function detect_unity()
{
    ps -e | grep -E 'unity-panel' > /dev/null
    if [ $? -ne 0 ];
    then
    return 0
    fi
    VERSION=`unity --version | awk '{print $2}'`
    DESKTOP="UNITY"
    return 1
}

function detect_xfce()
{
    ps -e | grep -E '^.* xfce4-session$' > /dev/null
    if [ $? -ne 0 ];
    then
    return 0
    fi
    VERSION=`xfce4-session --version | grep xfce4-session | awk '{print $2}'`
    DESKTOP="XFCE"
    return 1
}

function detect_cinnamon()
{
    ps -e | grep -E '^.* cinnamon$' > /dev/null
    if [ $? -ne 0 ];
    then
    return 0
    fi
    VERSION=`cinnamon --version | awk '{print $2}'`
    DESKTOP="CINNAMON"
    return 1
}

function detect_mate()
{
    ps -e | grep -E '^.* mate-panel$' > /dev/null
    if [ $? -ne 0 ];
    then
    return 0
    fi
    VERSION=`mate-about --version | awk '{print $4}'`
    DESKTOP="MATE"
    return 1
}

function detect_lxde()
{
    ps -e | grep -E '^.* lxsession$' > /dev/null
    if [ $? -ne 0 ];
    then
    return 0
    fi

    # We can detect LXDE version only thru package manager
    which apt-cache > /dev/null 2> /dev/null
    if [ $? -ne 0 ];
    then
    which yum > /dev/null 2> /dev/null
    if [ $? -ne 0 ];
    then
        VERSION='UNKNOWN'
    else
        # For Fedora
        VERSION=`yum list lxde-common | grep lxde-common | awk '{print $2}' | awk -F '-' '{print $1}'`
    fi
    else    
    # For Lubuntu and Knoppix
    VERSION=`apt-cache show lxde-common /| grep 'Version:' | awk '{print $2}' | awk -F '-' '{print $1}'`
    fi
    DESKTOP="LXDE"
    return 1
}

function detect_sugar()
{
    if [ "$DESKTOP_SESSION" == "sugar" ];
    then
    VERSION=`python -c "from jarabe import config; print config.version"`
    DESKTOP="SUGAR"
    else
    return 0
    fi
}


DESKTOP="UNKNOWN"
if detect_unity;
then
    if detect_kde;
    then
    if detect_gnome;
    then
        if detect_xfce;
        then
        if detect_cinnamon;
        then
            if detect_mate;
            then
            if detect_lxde;
            then
                detect_sugar
            fi
            fi
        fi
        fi
    fi
    fi
fi


if [ "$1" == '-v' ];
then
    echo $VERSION
else
    if [ "$1" == '-n' ];
    then
    echo $DESKTOP
    else
    echo $DESKTOP $VERSION
    fi
fi

share|improve this answer

  • Works on Arch Linux

    – Colin Keenan
    Oct 7 ’14 at 15:20

  • I stole your idea to check just for Xfce in my script: ps -e | grep -E '^.* xfce4-session$' > /dev/null (($? == 0)) && ...

    – Colin Keenan
    Oct 7 ’14 at 15:42

  • 1

    this did not work for me on GNOME Shell 3.26.2 I got UNKNOWN. No output from gnome-session --version working for me is gnome-shell --version | awk '{print $3}' I also did not get anything out of ps -e | grep -E '^.* gnome-session$'. This seems to be because of -b at the end of gnome-session-b. Removing the $ works or just simply ps -e | grep 'gnome-session'. With this changes the script is working. I get GNOME 3.26.2

    – nath
    Dec 22 ’17 at 1:01

1

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 ’14 at 16:16

  • 1

    @on4aa It’s echo ${DESKTOP_SESSION} kde-plasma-safe for my KDE. Whyever “safe”…

    – Hauke Laging
    Feb 23 ’14 at 16:23

  • Are there some Linux Mint Mate/Cinnamon users here? E17, LXDE, etc. are also welcome…

    – Serge Stroobandt
    Feb 23 ’14 at 16:32

1

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 ’14 at 23:37

  • Indeed, with emphasis on “if available”. See also this Askubuntu answer.

    – Serge Stroobandt
    Feb 24 ’14 at 8:16

1

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 ’14 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 ’14 at 9:39

  • @Greame You are right; I was a bit too quick and overlooked the unfinished another_desktop outcome.

    – Serge Stroobandt
    Feb 24 ’14 at 10:33

1

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 ’14 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 ’14 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 ’14 at 16:35

1

This works on both Ubuntu 18.10 (cosmic) [lxqt type] and Mint 19 (tara) [cinnamon type]:

$  set | grep XDG_CURRENT_DESKTOP | tr '[:upper:]' '[:lower:]' | cut -d'=' -f2

Capture the result into a variable, and execute further necessary code via capable analytic logic (case stmt, if/then) which includes any/all recognized types from there; and/or function gracefully when unrecognized types are in use.

share|improve this answer

  • Works also with Xubuntu LTS 18.04.

    – Serge Stroobandt
    Dec 31 ’18 at 18:52

Your Answer

StackExchange.ready(function() {
var channelOptions = {
tags: “”.split(” “),
id: “106”
};
initTagRenderer(“”.split(” “), “”.split(” “), channelOptions);

StackExchange.using(“externalEditor”, function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using(“snippets”, function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: ‘answer’,
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: “”,
imageUploader: {
brandingHtml: “Powered by u003ca class=”icon-imgur-white” href=”https://imgur.com/”u003eu003c/au003e”,
contentPolicyHtml: “User contributions licensed under u003ca href=”https://creativecommons.org/licenses/by-sa/3.0/”u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href=”https://stackoverflow.com/legal/content-policy”u003e(content policy)u003c/au003e”,
allowUrls: true
},
onDemand: true,
discardSelector: “.discard-answer”
,immediatelyShowMarkdownHelp:true
});

}
});

draft saved
draft discarded

StackExchange.ready(
function () {
StackExchange.openid.initPostLogin(‘.new-post-login’, ‘https%3a%2f%2funix.stackexchange.com%2fquestions%2f116539%2fhow-to-detect-the-desktop-environment-in-a-bash-script%23new-answer’, ‘question_page’);
}
);

Post as a guest

Required, but never shown

8 Answers
8

active

oldest

votes

8 Answers
8

active

oldest

votes

active

oldest

votes

active

oldest

votes

11

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 ’14 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 ’14 at 17:38

  • 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 ’14 at 16:34

  • @slm interesting, does XDG_DATA_DIRS not exist or does it just not contain anything useful?

    – Graeme
    Feb 26 ’14 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 ’14 at 17:02

11

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 ’14 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 ’14 at 17:38

  • 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 ’14 at 16:34

  • @slm interesting, does XDG_DATA_DIRS not exist or does it just not contain anything useful?

    – Graeme
    Feb 26 ’14 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 ’14 at 17:02

11

11

11

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

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

share|improve this answer

share|improve this answer

edited Apr 13 ’17 at 12:22

Community

1

1

answered Feb 24 ’14 at 13:01

GraemeGraeme

25k46497

25k46497

  • 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 ’14 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 ’14 at 17:38

  • 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 ’14 at 16:34

  • @slm interesting, does XDG_DATA_DIRS not exist or does it just not contain anything useful?

    – Graeme
    Feb 26 ’14 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 ’14 at 17:02

  • 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 ’14 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 ’14 at 17:38

  • 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 ’14 at 16:34

  • @slm interesting, does XDG_DATA_DIRS not exist or does it just not contain anything useful?

    – Graeme
    Feb 26 ’14 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 ’14 at 17:02

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 ’14 at 17:05

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 ’14 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 ’14 at 17:38

@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 ’14 at 17:38

1

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 ’14 at 16:34

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 ’14 at 16:34

@slm interesting, does XDG_DATA_DIRS not exist or does it just not contain anything useful?

– Graeme
Feb 26 ’14 at 16:45

@slm interesting, does XDG_DATA_DIRS not exist or does it just not contain anything useful?

– Graeme
Feb 26 ’14 at 16:45

1

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 ’14 at 17:02

@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 ’14 at 17:02

10

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

Another choice is the $XDG_SESSION_DESKTOP variable.

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

  • How to determine which window manager is running
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 ’14 at 17:04

  • 1

    @on4aa DESKTOP_SESSION on xfce.

    – Graeme
    Feb 23 ’14 at 17:38

  • 1

    on xfce, my DESKTOP_SESSION is default (mint 15)

    – glenn jackman
    Feb 23 ’14 at 18:34

  • 1

    Debian showed default for this too. A default install of GNOME 3.

    – slm
    Feb 23 ’14 at 18:44

  • 1

    $DESKTOP_SESSION shows default for KDE under Fedora 20.

    – Matthew Cline
    Feb 23 ’14 at 22:51

10

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

Another choice is the $XDG_SESSION_DESKTOP variable.

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

  • How to determine which window manager is running
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 ’14 at 17:04

  • 1

    @on4aa DESKTOP_SESSION on xfce.

    – Graeme
    Feb 23 ’14 at 17:38

  • 1

    on xfce, my DESKTOP_SESSION is default (mint 15)

    – glenn jackman
    Feb 23 ’14 at 18:34

  • 1

    Debian showed default for this too. A default install of GNOME 3.

    – slm
    Feb 23 ’14 at 18:44

  • 1

    $DESKTOP_SESSION shows default for KDE under Fedora 20.

    – Matthew Cline
    Feb 23 ’14 at 22:51

10

10

10

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

Another choice is the $XDG_SESSION_DESKTOP variable.

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

  • How to determine which window manager is running
share|improve this answer

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

Another choice is the $XDG_SESSION_DESKTOP variable.

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

  • How to determine which window manager is running
share|improve this answer

share|improve this answer

share|improve this answer

edited Apr 13 ’17 at 12:22

Community

1

1

answered Feb 23 ’14 at 16:25

slmslm

248k66515678

248k66515678

  • 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 ’14 at 17:04

  • 1

    @on4aa DESKTOP_SESSION on xfce.

    – Graeme
    Feb 23 ’14 at 17:38

  • 1

    on xfce, my DESKTOP_SESSION is default (mint 15)

    – glenn jackman
    Feb 23 ’14 at 18:34

  • 1

    Debian showed default for this too. A default install of GNOME 3.

    – slm
    Feb 23 ’14 at 18:44

  • 1

    $DESKTOP_SESSION shows default for KDE under Fedora 20.

    – Matthew Cline
    Feb 23 ’14 at 22:51

  • 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 ’14 at 17:04

  • 1

    @on4aa DESKTOP_SESSION on xfce.

    – Graeme
    Feb 23 ’14 at 17:38

  • 1

    on xfce, my DESKTOP_SESSION is default (mint 15)

    – glenn jackman
    Feb 23 ’14 at 18:34

  • 1

    Debian showed default for this too. A default install of GNOME 3.

    – slm
    Feb 23 ’14 at 18:44

  • 1

    $DESKTOP_SESSION shows default for KDE under Fedora 20.

    – Matthew Cline
    Feb 23 ’14 at 22:51

1

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 ’14 at 17:04

@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 ’14 at 17:04

1

1

@on4aa DESKTOP_SESSION on xfce.

– Graeme
Feb 23 ’14 at 17:38

@on4aa DESKTOP_SESSION on xfce.

– Graeme
Feb 23 ’14 at 17:38

1

1

on xfce, my DESKTOP_SESSION is default (mint 15)

– glenn jackman
Feb 23 ’14 at 18:34

on xfce, my DESKTOP_SESSION is default (mint 15)

– glenn jackman
Feb 23 ’14 at 18:34

1

1

Debian showed default for this too. A default install of GNOME 3.

– slm
Feb 23 ’14 at 18:44

Debian showed default for this too. A default install of GNOME 3.

– slm
Feb 23 ’14 at 18:44

1

1

$DESKTOP_SESSION shows default for KDE under Fedora 20.

– Matthew Cline
Feb 23 ’14 at 22:51

$DESKTOP_SESSION shows default for KDE under Fedora 20.

– Matthew Cline
Feb 23 ’14 at 22:51

5

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

#!/bin/bash

function detect_gnome()
{
    ps -e | grep -E '^.* gnome-session$' > /dev/null
    if [ $? -ne 0 ];
    then
    return 0
    fi
    VERSION=`gnome-session --version | awk '{print $2}'`
    DESKTOP="GNOME"
    return 1
}

function detect_kde()
{
    ps -e | grep -E '^.* kded4$' > /dev/null
    if [ $? -ne 0 ];
    then
        return 0
    else    
        VERSION=`kded4 --version | grep -m 1 'KDE' | awk -F ':' '{print $2}' | awk '{print $1}'`
        DESKTOP="KDE"
        return 1
    fi
}

function detect_unity()
{
    ps -e | grep -E 'unity-panel' > /dev/null
    if [ $? -ne 0 ];
    then
    return 0
    fi
    VERSION=`unity --version | awk '{print $2}'`
    DESKTOP="UNITY"
    return 1
}

function detect_xfce()
{
    ps -e | grep -E '^.* xfce4-session$' > /dev/null
    if [ $? -ne 0 ];
    then
    return 0
    fi
    VERSION=`xfce4-session --version | grep xfce4-session | awk '{print $2}'`
    DESKTOP="XFCE"
    return 1
}

function detect_cinnamon()
{
    ps -e | grep -E '^.* cinnamon$' > /dev/null
    if [ $? -ne 0 ];
    then
    return 0
    fi
    VERSION=`cinnamon --version | awk '{print $2}'`
    DESKTOP="CINNAMON"
    return 1
}

function detect_mate()
{
    ps -e | grep -E '^.* mate-panel$' > /dev/null
    if [ $? -ne 0 ];
    then
    return 0
    fi
    VERSION=`mate-about --version | awk '{print $4}'`
    DESKTOP="MATE"
    return 1
}

function detect_lxde()
{
    ps -e | grep -E '^.* lxsession$' > /dev/null
    if [ $? -ne 0 ];
    then
    return 0
    fi

    # We can detect LXDE version only thru package manager
    which apt-cache > /dev/null 2> /dev/null
    if [ $? -ne 0 ];
    then
    which yum > /dev/null 2> /dev/null
    if [ $? -ne 0 ];
    then
        VERSION='UNKNOWN'
    else
        # For Fedora
        VERSION=`yum list lxde-common | grep lxde-common | awk '{print $2}' | awk -F '-' '{print $1}'`
    fi
    else    
    # For Lubuntu and Knoppix
    VERSION=`apt-cache show lxde-common /| grep 'Version:' | awk '{print $2}' | awk -F '-' '{print $1}'`
    fi
    DESKTOP="LXDE"
    return 1
}

function detect_sugar()
{
    if [ "$DESKTOP_SESSION" == "sugar" ];
    then
    VERSION=`python -c "from jarabe import config; print config.version"`
    DESKTOP="SUGAR"
    else
    return 0
    fi
}


DESKTOP="UNKNOWN"
if detect_unity;
then
    if detect_kde;
    then
    if detect_gnome;
    then
        if detect_xfce;
        then
        if detect_cinnamon;
        then
            if detect_mate;
            then
            if detect_lxde;
            then
                detect_sugar
            fi
            fi
        fi
        fi
    fi
    fi
fi


if [ "$1" == '-v' ];
then
    echo $VERSION
else
    if [ "$1" == '-n' ];
    then
    echo $DESKTOP
    else
    echo $DESKTOP $VERSION
    fi
fi

share|improve this answer

  • Works on Arch Linux

    – Colin Keenan
    Oct 7 ’14 at 15:20

  • I stole your idea to check just for Xfce in my script: ps -e | grep -E '^.* xfce4-session$' > /dev/null (($? == 0)) && ...

    – Colin Keenan
    Oct 7 ’14 at 15:42

  • 1

    this did not work for me on GNOME Shell 3.26.2 I got UNKNOWN. No output from gnome-session --version working for me is gnome-shell --version | awk '{print $3}' I also did not get anything out of ps -e | grep -E '^.* gnome-session$'. This seems to be because of -b at the end of gnome-session-b. Removing the $ works or just simply ps -e | grep 'gnome-session'. With this changes the script is working. I get GNOME 3.26.2

    – nath
    Dec 22 ’17 at 1:01

5

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

#!/bin/bash

function detect_gnome()
{
    ps -e | grep -E '^.* gnome-session$' > /dev/null
    if [ $? -ne 0 ];
    then
    return 0
    fi
    VERSION=`gnome-session --version | awk '{print $2}'`
    DESKTOP="GNOME"
    return 1
}

function detect_kde()
{
    ps -e | grep -E '^.* kded4$' > /dev/null
    if [ $? -ne 0 ];
    then
        return 0
    else    
        VERSION=`kded4 --version | grep -m 1 'KDE' | awk -F ':' '{print $2}' | awk '{print $1}'`
        DESKTOP="KDE"
        return 1
    fi
}

function detect_unity()
{
    ps -e | grep -E 'unity-panel' > /dev/null
    if [ $? -ne 0 ];
    then
    return 0
    fi
    VERSION=`unity --version | awk '{print $2}'`
    DESKTOP="UNITY"
    return 1
}

function detect_xfce()
{
    ps -e | grep -E '^.* xfce4-session$' > /dev/null
    if [ $? -ne 0 ];
    then
    return 0
    fi
    VERSION=`xfce4-session --version | grep xfce4-session | awk '{print $2}'`
    DESKTOP="XFCE"
    return 1
}

function detect_cinnamon()
{
    ps -e | grep -E '^.* cinnamon$' > /dev/null
    if [ $? -ne 0 ];
    then
    return 0
    fi
    VERSION=`cinnamon --version | awk '{print $2}'`
    DESKTOP="CINNAMON"
    return 1
}

function detect_mate()
{
    ps -e | grep -E '^.* mate-panel$' > /dev/null
    if [ $? -ne 0 ];
    then
    return 0
    fi
    VERSION=`mate-about --version | awk '{print $4}'`
    DESKTOP="MATE"
    return 1
}

function detect_lxde()
{
    ps -e | grep -E '^.* lxsession$' > /dev/null
    if [ $? -ne 0 ];
    then
    return 0
    fi

    # We can detect LXDE version only thru package manager
    which apt-cache > /dev/null 2> /dev/null
    if [ $? -ne 0 ];
    then
    which yum > /dev/null 2> /dev/null
    if [ $? -ne 0 ];
    then
        VERSION='UNKNOWN'
    else
        # For Fedora
        VERSION=`yum list lxde-common | grep lxde-common | awk '{print $2}' | awk -F '-' '{print $1}'`
    fi
    else    
    # For Lubuntu and Knoppix
    VERSION=`apt-cache show lxde-common /| grep 'Version:' | awk '{print $2}' | awk -F '-' '{print $1}'`
    fi
    DESKTOP="LXDE"
    return 1
}

function detect_sugar()
{
    if [ "$DESKTOP_SESSION" == "sugar" ];
    then
    VERSION=`python -c "from jarabe import config; print config.version"`
    DESKTOP="SUGAR"
    else
    return 0
    fi
}


DESKTOP="UNKNOWN"
if detect_unity;
then
    if detect_kde;
    then
    if detect_gnome;
    then
        if detect_xfce;
        then
        if detect_cinnamon;
        then
            if detect_mate;
            then
            if detect_lxde;
            then
                detect_sugar
            fi
            fi
        fi
        fi
    fi
    fi
fi


if [ "$1" == '-v' ];
then
    echo $VERSION
else
    if [ "$1" == '-n' ];
    then
    echo $DESKTOP
    else
    echo $DESKTOP $VERSION
    fi
fi

share|improve this answer

  • Works on Arch Linux

    – Colin Keenan
    Oct 7 ’14 at 15:20

  • I stole your idea to check just for Xfce in my script: ps -e | grep -E '^.* xfce4-session$' > /dev/null (($? == 0)) && ...

    – Colin Keenan
    Oct 7 ’14 at 15:42

  • 1

    this did not work for me on GNOME Shell 3.26.2 I got UNKNOWN. No output from gnome-session --version working for me is gnome-shell --version | awk '{print $3}' I also did not get anything out of ps -e | grep -E '^.* gnome-session$'. This seems to be because of -b at the end of gnome-session-b. Removing the $ works or just simply ps -e | grep 'gnome-session'. With this changes the script is working. I get GNOME 3.26.2

    – nath
    Dec 22 ’17 at 1:01

5

5

5

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

#!/bin/bash

function detect_gnome()
{
    ps -e | grep -E '^.* gnome-session$' > /dev/null
    if [ $? -ne 0 ];
    then
    return 0
    fi
    VERSION=`gnome-session --version | awk '{print $2}'`
    DESKTOP="GNOME"
    return 1
}

function detect_kde()
{
    ps -e | grep -E '^.* kded4$' > /dev/null
    if [ $? -ne 0 ];
    then
        return 0
    else    
        VERSION=`kded4 --version | grep -m 1 'KDE' | awk -F ':' '{print $2}' | awk '{print $1}'`
        DESKTOP="KDE"
        return 1
    fi
}

function detect_unity()
{
    ps -e | grep -E 'unity-panel' > /dev/null
    if [ $? -ne 0 ];
    then
    return 0
    fi
    VERSION=`unity --version | awk '{print $2}'`
    DESKTOP="UNITY"
    return 1
}

function detect_xfce()
{
    ps -e | grep -E '^.* xfce4-session$' > /dev/null
    if [ $? -ne 0 ];
    then
    return 0
    fi
    VERSION=`xfce4-session --version | grep xfce4-session | awk '{print $2}'`
    DESKTOP="XFCE"
    return 1
}

function detect_cinnamon()
{
    ps -e | grep -E '^.* cinnamon$' > /dev/null
    if [ $? -ne 0 ];
    then
    return 0
    fi
    VERSION=`cinnamon --version | awk '{print $2}'`
    DESKTOP="CINNAMON"
    return 1
}

function detect_mate()
{
    ps -e | grep -E '^.* mate-panel$' > /dev/null
    if [ $? -ne 0 ];
    then
    return 0
    fi
    VERSION=`mate-about --version | awk '{print $4}'`
    DESKTOP="MATE"
    return 1
}

function detect_lxde()
{
    ps -e | grep -E '^.* lxsession$' > /dev/null
    if [ $? -ne 0 ];
    then
    return 0
    fi

    # We can detect LXDE version only thru package manager
    which apt-cache > /dev/null 2> /dev/null
    if [ $? -ne 0 ];
    then
    which yum > /dev/null 2> /dev/null
    if [ $? -ne 0 ];
    then
        VERSION='UNKNOWN'
    else
        # For Fedora
        VERSION=`yum list lxde-common | grep lxde-common | awk '{print $2}' | awk -F '-' '{print $1}'`
    fi
    else    
    # For Lubuntu and Knoppix
    VERSION=`apt-cache show lxde-common /| grep 'Version:' | awk '{print $2}' | awk -F '-' '{print $1}'`
    fi
    DESKTOP="LXDE"
    return 1
}

function detect_sugar()
{
    if [ "$DESKTOP_SESSION" == "sugar" ];
    then
    VERSION=`python -c "from jarabe import config; print config.version"`
    DESKTOP="SUGAR"
    else
    return 0
    fi
}


DESKTOP="UNKNOWN"
if detect_unity;
then
    if detect_kde;
    then
    if detect_gnome;
    then
        if detect_xfce;
        then
        if detect_cinnamon;
        then
            if detect_mate;
            then
            if detect_lxde;
            then
                detect_sugar
            fi
            fi
        fi
        fi
    fi
    fi
fi


if [ "$1" == '-v' ];
then
    echo $VERSION
else
    if [ "$1" == '-n' ];
    then
    echo $DESKTOP
    else
    echo $DESKTOP $VERSION
    fi
fi

share|improve this answer

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

#!/bin/bash

function detect_gnome()
{
    ps -e | grep -E '^.* gnome-session$' > /dev/null
    if [ $? -ne 0 ];
    then
    return 0
    fi
    VERSION=`gnome-session --version | awk '{print $2}'`
    DESKTOP="GNOME"
    return 1
}

function detect_kde()
{
    ps -e | grep -E '^.* kded4$' > /dev/null
    if [ $? -ne 0 ];
    then
        return 0
    else    
        VERSION=`kded4 --version | grep -m 1 'KDE' | awk -F ':' '{print $2}' | awk '{print $1}'`
        DESKTOP="KDE"
        return 1
    fi
}

function detect_unity()
{
    ps -e | grep -E 'unity-panel' > /dev/null
    if [ $? -ne 0 ];
    then
    return 0
    fi
    VERSION=`unity --version | awk '{print $2}'`
    DESKTOP="UNITY"
    return 1
}

function detect_xfce()
{
    ps -e | grep -E '^.* xfce4-session$' > /dev/null
    if [ $? -ne 0 ];
    then
    return 0
    fi
    VERSION=`xfce4-session --version | grep xfce4-session | awk '{print $2}'`
    DESKTOP="XFCE"
    return 1
}

function detect_cinnamon()
{
    ps -e | grep -E '^.* cinnamon$' > /dev/null
    if [ $? -ne 0 ];
    then
    return 0
    fi
    VERSION=`cinnamon --version | awk '{print $2}'`
    DESKTOP="CINNAMON"
    return 1
}

function detect_mate()
{
    ps -e | grep -E '^.* mate-panel$' > /dev/null
    if [ $? -ne 0 ];
    then
    return 0
    fi
    VERSION=`mate-about --version | awk '{print $4}'`
    DESKTOP="MATE"
    return 1
}

function detect_lxde()
{
    ps -e | grep -E '^.* lxsession$' > /dev/null
    if [ $? -ne 0 ];
    then
    return 0
    fi

    # We can detect LXDE version only thru package manager
    which apt-cache > /dev/null 2> /dev/null
    if [ $? -ne 0 ];
    then
    which yum > /dev/null 2> /dev/null
    if [ $? -ne 0 ];
    then
        VERSION='UNKNOWN'
    else
        # For Fedora
        VERSION=`yum list lxde-common | grep lxde-common | awk '{print $2}' | awk -F '-' '{print $1}'`
    fi
    else    
    # For Lubuntu and Knoppix
    VERSION=`apt-cache show lxde-common /| grep 'Version:' | awk '{print $2}' | awk -F '-' '{print $1}'`
    fi
    DESKTOP="LXDE"
    return 1
}

function detect_sugar()
{
    if [ "$DESKTOP_SESSION" == "sugar" ];
    then
    VERSION=`python -c "from jarabe import config; print config.version"`
    DESKTOP="SUGAR"
    else
    return 0
    fi
}


DESKTOP="UNKNOWN"
if detect_unity;
then
    if detect_kde;
    then
    if detect_gnome;
    then
        if detect_xfce;
        then
        if detect_cinnamon;
        then
            if detect_mate;
            then
            if detect_lxde;
            then
                detect_sugar
            fi
            fi
        fi
        fi
    fi
    fi
fi


if [ "$1" == '-v' ];
then
    echo $VERSION
else
    if [ "$1" == '-n' ];
    then
    echo $DESKTOP
    else
    echo $DESKTOP $VERSION
    fi
fi

share|improve this answer

share|improve this answer

share|improve this answer

edited Sep 17 ’15 at 10:43

terdon

129k32253428

129k32253428

answered Mar 18 ’14 at 15:16

alexeevdvalexeevdv

591

591

  • Works on Arch Linux

    – Colin Keenan
    Oct 7 ’14 at 15:20

  • I stole your idea to check just for Xfce in my script: ps -e | grep -E '^.* xfce4-session$' > /dev/null (($? == 0)) && ...

    – Colin Keenan
    Oct 7 ’14 at 15:42

  • 1

    this did not work for me on GNOME Shell 3.26.2 I got UNKNOWN. No output from gnome-session --version working for me is gnome-shell --version | awk '{print $3}' I also did not get anything out of ps -e | grep -E '^.* gnome-session$'. This seems to be because of -b at the end of gnome-session-b. Removing the $ works or just simply ps -e | grep 'gnome-session'. With this changes the script is working. I get GNOME 3.26.2

    – nath
    Dec 22 ’17 at 1:01

  • Works on Arch Linux

    – Colin Keenan
    Oct 7 ’14 at 15:20

  • I stole your idea to check just for Xfce in my script: ps -e | grep -E '^.* xfce4-session$' > /dev/null (($? == 0)) && ...

    – Colin Keenan
    Oct 7 ’14 at 15:42

  • 1

    this did not work for me on GNOME Shell 3.26.2 I got UNKNOWN. No output from gnome-session --version working for me is gnome-shell --version | awk '{print $3}' I also did not get anything out of ps -e | grep -E '^.* gnome-session$'. This seems to be because of -b at the end of gnome-session-b. Removing the $ works or just simply ps -e | grep 'gnome-session'. With this changes the script is working. I get GNOME 3.26.2

    – nath
    Dec 22 ’17 at 1:01

Works on Arch Linux

– Colin Keenan
Oct 7 ’14 at 15:20

Works on Arch Linux

– Colin Keenan
Oct 7 ’14 at 15:20

I stole your idea to check just for Xfce in my script: ps -e | grep -E '^.* xfce4-session$' > /dev/null (($? == 0)) && ...

– Colin Keenan
Oct 7 ’14 at 15:42

I stole your idea to check just for Xfce in my script: ps -e | grep -E '^.* xfce4-session$' > /dev/null (($? == 0)) && ...

– Colin Keenan
Oct 7 ’14 at 15:42

1

1

this did not work for me on GNOME Shell 3.26.2 I got UNKNOWN. No output from gnome-session --version working for me is gnome-shell --version | awk '{print $3}' I also did not get anything out of ps -e | grep -E '^.* gnome-session$'. This seems to be because of -b at the end of gnome-session-b. Removing the $ works or just simply ps -e | grep 'gnome-session'. With this changes the script is working. I get GNOME 3.26.2

– nath
Dec 22 ’17 at 1:01

this did not work for me on GNOME Shell 3.26.2 I got UNKNOWN. No output from gnome-session --version working for me is gnome-shell --version | awk '{print $3}' I also did not get anything out of ps -e | grep -E '^.* gnome-session$'. This seems to be because of -b at the end of gnome-session-b. Removing the $ works or just simply ps -e | grep 'gnome-session'. With this changes the script is working. I get GNOME 3.26.2

– nath
Dec 22 ’17 at 1:01

1

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 ’14 at 16:16

  • 1

    @on4aa It’s echo ${DESKTOP_SESSION} kde-plasma-safe for my KDE. Whyever “safe”…

    – Hauke Laging
    Feb 23 ’14 at 16:23

  • Are there some Linux Mint Mate/Cinnamon users here? E17, LXDE, etc. are also welcome…

    – Serge Stroobandt
    Feb 23 ’14 at 16:32

1

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 ’14 at 16:16

  • 1

    @on4aa It’s echo ${DESKTOP_SESSION} kde-plasma-safe for my KDE. Whyever “safe”…

    – Hauke Laging
    Feb 23 ’14 at 16:23

  • Are there some Linux Mint Mate/Cinnamon users here? E17, LXDE, etc. are also welcome…

    – Serge Stroobandt
    Feb 23 ’14 at 16:32

1

1

1

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

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

share|improve this answer

share|improve this answer

answered Feb 23 ’14 at 13:36

Hauke LagingHauke Laging

56k1285135

56k1285135

  • 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 ’14 at 16:16

  • 1

    @on4aa It’s echo ${DESKTOP_SESSION} kde-plasma-safe for my KDE. Whyever “safe”…

    – Hauke Laging
    Feb 23 ’14 at 16:23

  • Are there some Linux Mint Mate/Cinnamon users here? E17, LXDE, etc. are also welcome…

    – Serge Stroobandt
    Feb 23 ’14 at 16:32

  • 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 ’14 at 16:16

  • 1

    @on4aa It’s echo ${DESKTOP_SESSION} kde-plasma-safe for my KDE. Whyever “safe”…

    – Hauke Laging
    Feb 23 ’14 at 16:23

  • Are there some Linux Mint Mate/Cinnamon users here? E17, LXDE, etc. are also welcome…

    – Serge Stroobandt
    Feb 23 ’14 at 16:32

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 ’14 at 16:16

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 ’14 at 16:16

1

1

@on4aa It’s echo ${DESKTOP_SESSION} kde-plasma-safe for my KDE. Whyever “safe”…

– Hauke Laging
Feb 23 ’14 at 16:23

@on4aa It’s echo ${DESKTOP_SESSION} kde-plasma-safe for my KDE. Whyever “safe”…

– Hauke Laging
Feb 23 ’14 at 16:23

Are there some Linux Mint Mate/Cinnamon users here? E17, LXDE, etc. are also welcome…

– Serge Stroobandt
Feb 23 ’14 at 16:32

Are there some Linux Mint Mate/Cinnamon users here? E17, LXDE, etc. are also welcome…

– Serge Stroobandt
Feb 23 ’14 at 16:32

1

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 ’14 at 23:37

  • Indeed, with emphasis on “if available”. See also this Askubuntu answer.

    – Serge Stroobandt
    Feb 24 ’14 at 8:16

1

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 ’14 at 23:37

  • Indeed, with emphasis on “if available”. See also this Askubuntu answer.

    – Serge Stroobandt
    Feb 24 ’14 at 8:16

1

1

1

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

# echo $XDG_CURRENT_DESKTOP
KDE

share|improve this answer

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

# echo $XDG_CURRENT_DESKTOP
KDE

share|improve this answer

share|improve this answer

share|improve this answer

answered Feb 23 ’14 at 22:57

Matthew ClineMatthew Cline

1,31911124

1,31911124

  • 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 ’14 at 23:37

  • Indeed, with emphasis on “if available”. See also this Askubuntu answer.

    – Serge Stroobandt
    Feb 24 ’14 at 8:16

  • 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 ’14 at 23:37

  • Indeed, with emphasis on “if available”. See also this Askubuntu answer.

    – Serge Stroobandt
    Feb 24 ’14 at 8:16

1

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 ’14 at 23:37

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 ’14 at 23:37

Indeed, with emphasis on “if available”. See also this Askubuntu answer.

– Serge Stroobandt
Feb 24 ’14 at 8:16

Indeed, with emphasis on “if available”. See also this Askubuntu answer.

– Serge Stroobandt
Feb 24 ’14 at 8:16

1

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 ’14 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 ’14 at 9:39

  • @Greame You are right; I was a bit too quick and overlooked the unfinished another_desktop outcome.

    – Serge Stroobandt
    Feb 24 ’14 at 10:33

1

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 ’14 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 ’14 at 9:39

  • @Greame You are right; I was a bit too quick and overlooked the unfinished another_desktop outcome.

    – Serge Stroobandt
    Feb 24 ’14 at 10:33

1

1

1

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

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

share|improve this answer

share|improve this answer

edited Feb 24 ’14 at 9:43

answered Feb 23 ’14 at 15:34

GraemeGraeme

25k46497

25k46497

  • This returns unknown on Ubuntu.

    – Serge Stroobandt
    Feb 24 ’14 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 ’14 at 9:39

  • @Greame You are right; I was a bit too quick and overlooked the unfinished another_desktop outcome.

    – Serge Stroobandt
    Feb 24 ’14 at 10:33

  • This returns unknown on Ubuntu.

    – Serge Stroobandt
    Feb 24 ’14 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 ’14 at 9:39

  • @Greame You are right; I was a bit too quick and overlooked the unfinished another_desktop outcome.

    – Serge Stroobandt
    Feb 24 ’14 at 10:33

This returns unknown on Ubuntu.

– Serge Stroobandt
Feb 24 ’14 at 8:41

This returns unknown on Ubuntu.

– Serge Stroobandt
Feb 24 ’14 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 ’14 at 9:39

@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 ’14 at 9:39

@Greame You are right; I was a bit too quick and overlooked the unfinished another_desktop outcome.

– Serge Stroobandt
Feb 24 ’14 at 10:33

@Greame You are right; I was a bit too quick and overlooked the unfinished another_desktop outcome.

– Serge Stroobandt
Feb 24 ’14 at 10:33

1

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 ’14 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 ’14 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 ’14 at 16:35

1

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 ’14 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 ’14 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 ’14 at 16:35

1

1

1

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

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

share|improve this answer

share|improve this answer

edited Feb 24 ’14 at 16:51

answered Feb 24 ’14 at 8:40

Serge StroobandtSerge Stroobandt

81321325

81321325

  • 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 ’14 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 ’14 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 ’14 at 16:35

  • 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 ’14 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 ’14 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 ’14 at 16:35

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 ’14 at 10:14

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 ’14 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 ’14 at 10:15

Notably, if I specifically choose XFCE or Gnome from the display manager, DESKTOP_SESSION will actually say xfce or gnome

– Graeme
Feb 24 ’14 at 10:15

1

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 ’14 at 16:35

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 ’14 at 16:35

1

This works on both Ubuntu 18.10 (cosmic) [lxqt type] and Mint 19 (tara) [cinnamon type]:

$  set | grep XDG_CURRENT_DESKTOP | tr '[:upper:]' '[:lower:]' | cut -d'=' -f2

Capture the result into a variable, and execute further necessary code via capable analytic logic (case stmt, if/then) which includes any/all recognized types from there; and/or function gracefully when unrecognized types are in use.

share|improve this answer

  • Works also with Xubuntu LTS 18.04.

    – Serge Stroobandt
    Dec 31 ’18 at 18:52

1

This works on both Ubuntu 18.10 (cosmic) [lxqt type] and Mint 19 (tara) [cinnamon type]:

$  set | grep XDG_CURRENT_DESKTOP | tr '[:upper:]' '[:lower:]' | cut -d'=' -f2

Capture the result into a variable, and execute further necessary code via capable analytic logic (case stmt, if/then) which includes any/all recognized types from there; and/or function gracefully when unrecognized types are in use.

share|improve this answer

  • Works also with Xubuntu LTS 18.04.

    – Serge Stroobandt
    Dec 31 ’18 at 18:52

1

1

1

This works on both Ubuntu 18.10 (cosmic) [lxqt type] and Mint 19 (tara) [cinnamon type]:

$  set | grep XDG_CURRENT_DESKTOP | tr '[:upper:]' '[:lower:]' | cut -d'=' -f2

Capture the result into a variable, and execute further necessary code via capable analytic logic (case stmt, if/then) which includes any/all recognized types from there; and/or function gracefully when unrecognized types are in use.

share|improve this answer

This works on both Ubuntu 18.10 (cosmic) [lxqt type] and Mint 19 (tara) [cinnamon type]:

$  set | grep XDG_CURRENT_DESKTOP | tr '[:upper:]' '[:lower:]' | cut -d'=' -f2

Capture the result into a variable, and execute further necessary code via capable analytic logic (case stmt, if/then) which includes any/all recognized types from there; and/or function gracefully when unrecognized types are in use.

share|improve this answer

share|improve this answer

share|improve this answer

edited Dec 31 ’18 at 19:48

Serge Stroobandt

81321325

81321325

answered Dec 31 ’18 at 17:20

odoncaoaodoncaoa

112

112

  • Works also with Xubuntu LTS 18.04.

    – Serge Stroobandt
    Dec 31 ’18 at 18:52

  • Works also with Xubuntu LTS 18.04.

    – Serge Stroobandt
    Dec 31 ’18 at 18:52

Works also with Xubuntu LTS 18.04.

– Serge Stroobandt
Dec 31 ’18 at 18:52

Works also with Xubuntu LTS 18.04.

– Serge Stroobandt
Dec 31 ’18 at 18:52

draft saved
draft discarded

Thanks for contributing an answer to Unix & Linux Stack Exchange!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.

draft saved

draft discarded

StackExchange.ready(
function () {
StackExchange.openid.initPostLogin(‘.new-post-login’, ‘https%3a%2f%2funix.stackexchange.com%2fquestions%2f116539%2fhow-to-detect-the-desktop-environment-in-a-bash-script%23new-answer’, ‘question_page’);
}
);

Post as a guest

Required, but never shown

Required, but never shown

Required, but never shown

Required, but never shown

Required, but never shown

Required, but never shown

Required, but never shown

Required, but never shown

Required, but never shown

Related Post

Leave a Reply

Your email address will not be published. Required fields are marked *