Find the default terminal emulator

How would one detect which terminal emulator (xterm, gnome-terminal...) is used in the current desktop environment ? Like xdg-open, but for the terminal emulator.

I already tried the xdg-terminal script on my Xubuntu installation, with no luck :

./xdg-terminal.sh: 305: [: x: unexpected operator
./xdg-terminal.sh: 399: [: xxterm: unexpected operator
xdg-terminal: configured terminal program 'xterm' not found or not executable

It would be for using in a C++ program, so any script, package or built-in command will do.

Thanks for the help !


Solution 1:

Short answer

There is no standard for knowing what the default terminal emulators is across distros. In fact, the user might use 'by default' a completely different terminal than the one that ships with the desktop environment. You would only be able to guess it by looking into different system variables and config files.

Longer answer

You can try to guess your way forward with $TERM

Please refer to man term.5 and/or man term.7 (pages 5 and 7 of the term manual page).

The environment variable TERM should normally contain the type name of the terminal, console or display-device type you are using. This information is critical for all screen-oriented programs, including your editor and mailer.

A default TERM value will be set on a per-line basis by either /etc/inittab (Linux and System-V-like UNIXes) or /etc/ttys (BSD UNIXes). This will nearly always suffice for workstation and microcomputer consoles.

On my Manjaro i3 install

$ echo $TERM
rxvt-unicode-256color

which is the other name for urxvt. So you can't even expect to get the right name of the default terminal.

As detailed in this reply

If your $TERM has [something you don't recognise], carefully check your configuration, including:

  • the agetty lines in /etc/inittab (they should say linux at the end1)
    • system-wide shell startup scripts
      • /etc/profile, /etc/profile.d/*.sh
      • /etc/bash.bashrc (if using bash)
    • your own shell startup scripts
      • ~/.profile, ~/.bash_profile, ~/.bash_login, ~/.bashrc

Advice : just go through a list of known terminal emulators

By default i3 comes with i3-sensible-terminal. According to it's manual

i3-sensible-terminal is invoked in the i3 default config to start a terminal. This wrapper script is necessary since there is no distribution-independent terminal launcher (but for example Debian has x-terminal-emulator). Distribution packagers are responsible for shipping this script in a way which is appropriate for the distribution.

And the way it works is just by going through the list of commonly used terminal emulators

It tries to start one of the following (in that order):

  • $TERMINAL (this is a non-standard variable)
  • x-terminal-emulator (only present on Debian and derivatives)
  • urxvt
  • rxvt
  • termit
  • terminator
  • ...

Where

  • $TERMINAL is usually set in the aforementioned startup scripts when it is used.
  • x-terminal-emulator is the Debian way of asking for the default terminal (works on Ubuntu)

In a bash script, that would give something along the lines of

terms=(emulator1 emulator2 emulator3)
for t in ${terms[*]}
do
    if [ $(command -v $t) ]
    then
        detected_term=$t
        break
    fi
done
echo $detected_term