Thursday, May 2, 2024
35
rated 0 times [  35] [ 0]  / answers: 1 / hits: 42480  / 2 Years ago, wed, july 27, 2022, 4:42:11

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


More From » command-line

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



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



  1. 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.



  1. Combine everything in a single command


     ps -p $(ps -p $$ -o ppid=) -o args=


  2. Use that to get the version


     $(ps -p $(ps -p $$ -o ppid=) o args=) --version
    terminator 0.97


  3. 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 <[email protected]>
    and Nick Schermer <[email protected]>.

    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.


[#24814] Thursday, July 28, 2022, 2 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
ranquctive

Total Points: 391
Total Questions: 103
Total Answers: 104

Location: South Sudan
Member since Thu, Feb 4, 2021
3 Years ago
;