11

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.

7
  • 1
    Are you running that script with the correct shell? Those seem like the sort of errors I'd expect if you were using a different shell from the one in which the script was written or something like that Commented Dec 7, 2016 at 16:07
  • Do you really need to be able to identify which terminal emulator is the default? Or just launch it?
    – egmont
    Commented Dec 7, 2016 at 16:24
  • I just need to launch it
    – natinusala
    Commented Dec 7, 2016 at 16:34
  • 2
    On Debian/Ubuntu it's x-terminal-emulator. I don't know if it's generic across distros or not.
    – egmont
    Commented Dec 7, 2016 at 18:15
  • It doesn't seem to be
    – natinusala
    Commented Dec 8, 2016 at 20:34

1 Answer 1

7

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
1
  • Here are the i3 sources for this purpose. Maybe it should be included in the answer.
    – arthur.sw
    Commented Jun 8 at 9:17

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .