Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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.

Update

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)?

share|improve this question
    
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 at 17:48
    
Thanks but first problem with rxvt -> window 31457282 has no pid associated with it (tested on Lubuntu) –  TuKsn Jun 4 at 18:04
    
Oh? Damn, that's weird. I tested under Cinnamon. –  terdon Jun 4 at 18:05

5 Answers 5

up vote 19 down vote accepted

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=
    544
    

    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):

    which_term(){
        term=$(ps -p $(ps -p $$ -o ppid=) -o args=);
        found=0;
        case $term in
            *gnome-terminal*)
                found=1
                echo "gnome-terminal " $(dpkg -l gnome-terminal | awk '/^ii/{print $3}')
                ;;
            *lxterminal*)
                found=1
                echo "lxterminal " $(dpkg -l lxterminal | awk '/^ii/{print $3}')
                ;;
            rxvt*)
                found=1
                echo "rxvt " $(dpkg -l rxvt | awk '/^ii/{print $3}')
                ;;
            ## Try and guess for any others
            *)
                for v in '-version' '--version' '-V' '-v'
                do
                    $term "$v" &>/dev/null && eval $term $v && found=1 && break
                done
                ;;
        esac
        ## 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
    XTerm(297)
    
  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 
lxterminal 

This means that getting the version is trivial. For example

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

Not so for gnome-terminal:

$ which_term 
/usr/lib/gnome-terminal/gnome-terminal-server 

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):

which_term(){
    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* )
          term="gnome-terminal"
        ;;
        ## For other cases, just take the 1st
        ## field of $term
        * )
          term=${term/% */}
        ;;
     esac
     version=$(dpkg -l "$term" | awk '/^ii/{print $3}')
     echo "$term  $version"
}

This works for all cases I tested on.

share|improve this answer
    
@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 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 at 14:06
2  
You can just use $PPID to get the PID of the parent process. –  nyuszika7h Jun 4 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 at 16:45
    
It's definitely available in ksh93 and mksh. –  nyuszika7h Jun 5 at 19:23

Try this,

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

OR

ps -aux | grep `ps -p $$ -o ppid=` | awk 'NR==1{print $NF}'
share|improve this answer
    
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 at 12:16
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.

Caveats

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.

share|improve this answer
    
This is very nice +1, but on the terminator terminal i get only termin as output. –  TuKsn Jun 4 at 17:06
    
I don't know why, it's probably Terminator's fault. –  nyuszika7h Jun 4 at 17:23
    
It's because it's run as a python script I think. It's really annoying. –  terdon Jun 4 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 at 23:05

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
share|improve this answer
    
I think he wants to know it for the active terminal session :) So if he is using "putty" at that time ... –  Rinzwind Jun 4 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 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 at 10:12
    
The Update works well also with xterm and shows the version and the name. –  TuKsn Jun 4 at 10:39
    
All the credit goes to @Avinash and you for the right command. I've just added the version. –  Sylvain Pineau Jun 4 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".

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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