I'm looking for how to get the name of the terminal emulator (« aterm », « xterm », « konsole », etc.) with C Programming langage (GNU/Linux). I have done several researches, but I didn't find anything.
5 Answers
I doubt there's a reliable way to check this.
As @larsmans suggested, you can check the TERM
env variable, but most emulators use the same terminal setting..
I would check the parent process id (getppid
) and its parent ( linux: programmatically get parent pid of another process? ) and so on till you find a process with a name that looks like a terminal emulator...
Or, if you have pstree
simply process its output:
# pstree -As 12652
init---screen---bash---pstree
(sorry, I don't have X terminal so I cannot show a proper example)
Anyway, none of these are totally reliable but probably will work.

- 1
- 1

- 94,607
- 11
- 117
- 176
-
It's probably not 100% reliable, but I can confirm that `getenv()` will produce the terminal emulator. – Makoto May 09 '12 at 16:28
-
mine shows `xterm`, but it's just an ssh connection (with putty) – Karoly Horvath May 09 '12 at 16:33
-
@KarolyHorvath: Take a look at my [answer](http://stackoverflow.com/a/15309583/1050454), I implemented your method. – Emanuel Berg Mar 09 '13 at 10:46
The name of the terminal is given by the TERM
environment variable.
char *termtype = getenv("TERM");
printf("The terminal type is %s\n", termtype);

- 8,344
- 1
- 29
- 36
-
3as you said, that's the name of the terminal (which just tells you its capabilities), and not the terminal *emulator*. – Karoly Horvath May 09 '12 at 16:36
-
1
See if this works. I tested it from Emacs, and in urxvt, xterm, and rxvt.
#!/bin/zsh
terminal_emulator_parents=`pstree -As $$`
tep_list=`echo $terminal_emulator_parents | tr -s "-" | tr "-" " " \
| tac -s' ' | tr '\n' ' '`
found="false"
for process in `echo $tep_list`; do
if [[ $process =~ ("urxvt"|"xterm"|"rxvt") ]]; then # here: add all
found="true" # terminal emulators
break # you can think of
fi
done
if [[ $found == "true" ]]; then
echo "Terminal emulator: $process"
else
echo "Couldn't determine the terminal emulator."
fi

- 499
- 4
- 14
-
1This was really helpful and I used it to create a solution that works well for me. – Dannid Jun 05 '14 at 21:03
Building off the prior two answers with pstree, I created an environment variable to identify the terminal emulator type in my .bashrc, then in my script, I can access that information.
I use this to set my guake tab title to the current virtualenvironment using postactivate and clear it in postdeactivate, but I don't want guake to be invoked if I happen to enter a virtualenvironment when I'm using a different terminal program.
Here's my code:
in .bashrc
export TERM_TYPE=`pstree -As $$ | awk -F "---" '{print $2}'`
in postdeactivate or postactivate (replace "Terminal" with the desired tab name)
#!/bin/bash
# This hook is run after every virtualenv is deactivated.
if [ "$TERM_TYPE" == "guake" ]; then
guake -r Terminal
fi

- 1,507
- 1
- 20
- 17
There are the functions: ctermid() and gettyname()
ctermid returns the name in the specified string
getttyname works on the selected file device

- 1