How can I get the name of the current terminal from command-line?
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)?
Solution 1:
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.
-
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 tops
(-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
. -
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
. -
Combine everything in a single command
ps -p $(ps -p $$ -o ppid=) o args=
-
Use that to get the version
$(ps -p $(ps -p $$ -o ppid=) o args=) --version terminator 0.97
-
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:
-
xterm
$ which_term XTerm(297)
-
terminator
$ which_term terminator 0.97
-
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
-
gnome-terminal
.$ which_term gnome-terminal 3.10.1-1
-
konsole
$ which_term Qt: 4.8.6 KDE Development Platform: 4.11.3 Konsole: 2.11.3
-
lxterminal
$ which_term lxterminal 0.1.11-4
-
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 su
ing 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.
Solution 2:
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 technicallyargv[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.
Solution 3:
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}'