Ubuntu – How to get current gnome keyboard layout from terminal

command linekeyboard-layout

For usage in a bash script, I need to get the gnome keyboard layout the user is currently using. For example if the user sets its keyboard layout to en-us , I need a bash command that prints me this.

How can I get that information?

Update:

setxkbmap -query is unfortunatelly not working. Below is the ouput with the en (first command) and the de (second command) layout activated. Switching keyboard layout seems to be have some relation with gnome session configuration

setxkbmap -query 
rules:      evdev
model:      pc105
layout:     us,de
variant:    ,
options:    terminate:ctrl_alt_bksp,lv3:ralt_switch,grp:alts_toggle

setxkbmap -query
rules:      evdev
model:      pc105
layout:     us,de
variant:    ,
options:    terminate:ctrl_alt_bksp,lv3:ralt_switch,grp:alts_toggle

Update2:

setxkbmap -print #with en-us layout
xkb_keymap {
    xkb_keycodes  { include "evdev+aliases(qwerty)" };
    xkb_types     { include "complete"  };
    xkb_compat    { include "complete"  };
    xkb_symbols   { include "pc+us+de:2+inet(evdev)+level3(ralt_switch_for_alts_toggle):1+level3(ralt_switch_for_alts_toggle):2+group(alts_toggle)+level3(ralt_switch)+terminate(ctrl_alt_bksp)"    };
    xkb_geometry  { include "pc(pc105)" };
};
setxkbmap -print #after switching to german layout
xkb_keymap {
    xkb_keycodes  { include "evdev+aliases(qwerty)" };
    xkb_types     { include "complete"  };
    xkb_compat    { include "complete"  };
    xkb_symbols   { include "pc+us+de:2+inet(evdev)+level3(ralt_switch_for_alts_toggle):1+level3(ralt_switch_for_alts_toggle):2+group(alts_toggle)+level3(ralt_switch)+terminate(ctrl_alt_bksp)"    };
    xkb_geometry  { include "pc(pc105)" };
};

Best Answer

According to a similar question on Stackoverflow, the following should do the trick:

setxkbmap -print | grep xkb_symbols | awk '{print $4}' | awk -F"+" '{print $2}'

I did could not verify it, as I currently have no *nix machine with X available (I'm not home)...