Is there a possibility to get the type of terminal with a command?

If I'm using gnome-terminal, the output should be gnome-terminal or something similar. It would be also nice to get the version of the terminal.


ps -aux | grep `ps -p $$ -o ppid=` 

will output something like this:

user     4239  0.0  0.7 292708 15744 pts/8    Sl   11:39   0:02 xfce4-terminal
user     4800  0.0  0.0   6176   820 pts/0    S+   12:23   0:00 grep --color=auto  4239

This will also work with xterm, but how do I get only the name (xfce4-terminal in this case)?

  • I just updated my answer with a different and better approach. If you have the time, have a look and let me know if it also works for you.
    – terdon
    Jun 4, 2014 at 17:48
  • Thanks but first problem with rxvt -> window 31457282 has no pid associated with it (tested on Lubuntu)
    – TuKsn
    Jun 4, 2014 at 18:04
  • Oh? Damn, that's weird. I tested under Cinnamon.
    – terdon
    Jun 4, 2014 at 18:05

7 Answers 7


Original version

One way to do this is to get the parent process of your current shell session and from there the name of the terminal.

  1. Get the parent of the current shell process. The bash variable $$ is the PID of your current shell, so we can give that as a query to ps (-p $$) and ask it tp print the PID of the parent process (-o ppid=, the trailing = is to avoid printing column headers):

    $ ps -p $$ -o ppid=

    So, the PID of my shell's parent is 544.

  2. Get the process associated with that PID and print its command line

    $ ps -p 544 o args=
    /usr/bin/python /usr/bin/terminator

    The above output will depend on what terminal emulator you are using, I am using terminator.

  3. Combine everything in a single command

    ps -p $(ps -p $$ -o ppid=) o args=
  4. Use that to get the version

    $(ps -p $(ps -p $$ -o ppid=) o args=) --version
    terminator 0.97
  5. Add a little function to your ~/.bashrc that returns the name and version of the terminal emulator you're using (this works for most common terminal emulators):

        term=$(ps -p $(ps -p $$ -o ppid=) -o args=);
        case $term in
                echo "gnome-terminal " $(dpkg -l gnome-terminal | awk '/^ii/{print $3}')
                echo "lxterminal " $(dpkg -l lxterminal | awk '/^ii/{print $3}')
                echo "rxvt " $(dpkg -l rxvt | awk '/^ii/{print $3}')
            ## Try and guess for any others
                for v in '-version' '--version' '-V' '-v'
                    $term "$v" &>/dev/null && eval $term $v && found=1 && break
        ## If none of the version arguments worked, try and get the 
        ## package version
        [ $found -eq 0 ] && echo "$term " $(dpkg -l $term | awk '/^ii/{print $3}')    

    You can now get the name of the terminal and also pass any option you like to it (such as --version.

Some examples using different terminals:

  1. xterm

    $ which_term
  2. terminator

    $ which_term 
    terminator 0.97
  3. rxvt, this one has none of the -V, -version or --version flags so no version info is printed.

    $  which_term
    rxvt  1:2.7.10-5
  4. gnome-terminal.

    $ which_term
    gnome-terminal  3.10.1-1
  5. konsole

    $ which_term
    Qt: 4.8.6
    KDE Development Platform: 4.11.3
    Konsole: 2.11.3
  6. lxterminal

    $ which_term
    lxterminal  0.1.11-4
  7. xfce4-terminal

    $ which_term
    xfce4-terminal 0.6.2 (Xfce 4.10)
    Copyright (c) 2003-2012
        The Xfce development team. All rights reserved.
    Written by Benedikt Meurer <benny@xfce.org>
    and Nick Schermer <nick@xfce.org>.
    Please report bugs to <http://bugzilla.xfce.org/>.

New and improved

The above approach is not that trustworthy though. It will choke when you run your shell after suing to another user or when your terminal is aliased to something and various other cases. Since we are obviously working with X programs here, a better way might be to use something like xdotool (installable with sudo apt-get install xdotool) to get the information instead:

perl -lpe 's/\0/ /g' /proc/$(xdotool getwindowpid $(xdotool getactivewindow))/cmdline

The above will print the command line used to launch the currently active window. Since your terminal will, presumably, be active, that is the command it will show. This means that for most terminal emulators, you can safely assume that the 1st field returned is the terminal name:

$ which_term 

This means that getting the version is trivial. For example

$ dpkg -l $(which_term) | awk '/^ii/{print $3}'

Not so for gnome-terminal:

$ which_term 

or terminator:

$ which_term
/usr/bin/python /usr/bin/terminator 

So, we can make it a little more complex (there are some bashisms here, this one is not portable):

    term=$(perl -lpe 's/\0/ /g' \
           /proc/$(xdotool getwindowpid $(xdotool getactivewindow))/cmdline)

    ## Enable extended globbing patterns
    shopt -s extglob
    case $term in
        ## If this terminal is a python or perl program,
        ## then the emulator's name is likely the second 
        ## part of it
        */python*|*/perl*    )
         term=$(basename "$(readlink -f $(echo "$term" | cut -d ' ' -f 2))")
         version=$(dpkg -l "$term" | awk '/^ii/{print $3}')
        ## The special case of gnome-terminal
        *gnome-terminal-server* )
        ## For other cases, just take the 1st
        ## field of $term
        * )
          term=${term/% */}
     version=$(dpkg -l "$term" | awk '/^ii/{print $3}')
     echo "$term  $version"

This works for all cases I tested on.

  • @Xubu-Tur hmm, I only tried by launching each terminal from another terminal manually and it worked fine. I'm guessing your desktop environment menus have a different path or somehow differ in how they launch the terminals. I might be able to give you something better if you tell me exactly how you launched them and what error you got. If you launched from a menu, check what command the menu item runs.
    – terdon
    Jun 4, 2014 at 14:02
  • @Xubu-Tur don't add a screenshot. Just paste the error message into your question. Same for whatever command the menu item launches. Don't expect me to manually copy it from a picture!
    – terdon
    Jun 4, 2014 at 14:06
  • 2
    You can just use $PPID to get the PID of the parent process.
    – nyuszika7h
    Jun 4, 2014 at 14:53
  • @nyuszika7h yes you can and +1 for that but I'd rather not. I just checked and it's not available in ksh or tcsh. It's a very good idea for the bash family though since it is much simpler than my approach.
    – terdon
    Jun 4, 2014 at 16:45
  • It's definitely available in ksh93 and mksh.
    – nyuszika7h
    Jun 5, 2014 at 19:23
basename "$(cat "/proc/$PPID/comm")"

$PPID is the PID of the shell's parent process. comm means command. It may or may not be a full path, so we use basename to strip the path if needed.


These probably apply to at least some of the other answers too.

  • comm is technically argv[0], which can actually be an arbitrary string. But in general, you should be able to rely on it for this particular case.

  • This won't work as expected if you connect over SSH or use tmux, screen or something similar.

  • This is very nice +1, but on the terminator terminal i get only termin as output.
    – TuKsn
    Jun 4, 2014 at 17:06
  • I don't know why, it's probably Terminator's fault.
    – nyuszika7h
    Jun 4, 2014 at 17:23
  • It's because it's run as a python script I think. It's really annoying.
    – terdon
    Jun 4, 2014 at 18:04
  • Please note that $PPID is not standard in all shells. e.g. in tcsh I get: PPID: Undefined variable.
    – arielf
    Jun 6, 2014 at 23:05

Try this,

ps -aux | grep `ps -p $$ -o ppid=` | awk 'NR==1{print $11}'


ps -aux | grep `ps -p $$ -o ppid=` | awk 'NR==1{print $NF}'
  • The first fails on rxvt and terminator and the second fails on uxterm and gnome-terminal (it prints /usr/lib/gnome-terminal/gnome-terminal-server). They will all fail (as will mine) if you are using an su session.
    – terdon
    Jun 4, 2014 at 12:16

You can try the following command:

$ dpkg-query -W $COLORTERM
gnome-terminal  3.6.2-0ubuntu1

Update (thanks to OP and Avinash Raj):

$ dpkg-query -W $(ps -aux | grep "`ps -p $$ -o ppid=`" | awk 'NR==1{print $11}' | xargs basename)
rxvt    1:2.7.10-5
  • I think he wants to know it for the active terminal session :) So if he is using "putty" at that time ...
    – Rinzwind
    Jun 4, 2014 at 10:02
  • No not over "putty" but whats about xterm, with this command i get also gnome-terminal 3.6.2-0ubuntu1l in xterm?
    – TuKsn
    Jun 4, 2014 at 10:08
  • 2
    This does not print the terminal you are using at the moment. It prints whatever has been set s the default $COLORTERM, that has nothing to do with what you're using at the time.
    – terdon
    Jun 4, 2014 at 10:12
  • The Update works well also with xterm and shows the version and the name.
    – TuKsn
    Jun 4, 2014 at 10:39
  • All the credit goes to @Avinash and you for the right command. I've just added the version. Jun 4, 2014 at 10:41

Another (not perfect) possibility is:

xprop -id $WINDOWID WM_CLASS | cut -d" " -f3 | sed 's/^.\(.*\)..$/\1/'

But this does not work with lxterminal because there is the environment variable $WINDOWID empty...

Terminator shows the name "x-terminal-emulator".


The name of the terminal might be set in your environment variables.

echo $TERM


$ echo $TERM
  • -1, It specifies what sort of terminal you're using, not the terminal itself. So for instance, I'm using Konsole, but echo $TERM prints xterm-256color.
    – aderchox
    Jul 26, 2020 at 14:17

The current terminal can also be detected via an escape sequence: The Device Attributes (Secondary DA)

For example in bash:

read -s -dc -p $'\E[>c' da;da=${da##$'\E'[>};echo "${da//;/ }"

This returns only some kind of id - and sadly I don't know an official list that translate these to the actual names.

For a real implementation, that translates some known ids to terminal names, see this example bash script: https://github.com/mintty/utils/blob/master/terminal

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.