How do I check which terminal I am using?
Solution 1:
TL;DR
- to find currently running shell use
ls -l /proc/$$/exe
- to find currently running terminal, use
xprop _NET_WM_PID WM_CLASS
. The value ofpid
later can be passed tops -p <pid> -o args
command. -
Technically, for terminal emulator you don't even need a command, as stated in the comments:
what do you mean by which? Click Help --> About is that it? – JoKeR
Shell vs Terminal
First thing we need to have clarified is what exactly is being asked - find out the running shell or running terminal. Often these two terms are used interchangeably, but they are different things altogether. Shell is the command-line interpreter, specifically interactive shell is the prompt plus text field where you enter commands. Shells can also be non-interactive, for example a script starts non-interactive shell, or bash -c 'echo hello world'
also starts non-interactive shell.
By contrast, terminal is the interface to shell ( though it could be another application as well). Originally terminal referred to actual hardware, but nowadays they're mostly software. What you see when you press Ctrl+Alt+t or click on the terminal icon in GUI, that starts a terminal emulator, a window which mimics behavior of hardware, and within that window you can see the shell running. Ctrl+Alt+F2 (or any of the 6 function keys) will open virtual console, aka tty
. I recommend reading Why is a virtual terminal “virtual”, and what/why/where is the “real” terminal? for more info on the specifics.
Getting the shell information
Each user has a default shell assigned to them in /etc/passwd
for their username. Assuming you are using default configuration and haven't called another shell explicitly as a command, it is sufficient to do:
echo $SHELL
But of course this only shows default value. Suppose we do the following:
user@ubuntu:~$ dash
$
We were originally in bash
, but started interactive session of /bin/dash
, Ubuntu's POSIX or system shell. The variable $SHELL
won't change, because that's not its purpose - it shows default not current value. We will need to approach this from another perspective - the perspective of a process, which is something I've covered in Am I using bash or sh?
$ echo $$
4824
$ cat /proc/4824/comm
mksh
$ bash
xieerqi@eagle:~$ echo $$
6197
xieerqi@eagle:~$ cat /proc/6197/comm
bash
Here we take advantage of /proc/
filesystem. The name of the process and command-line parameters are displayed in /proc/<pid>/comm
. All we need is to provide shell's PID, which is what $$
does. In the example above I am adding that separately, but there is nothing stopping us from doing just
cat /proc/$$/comm
Variation on the theme could also be
ps -p $$ -o args
Another way we could approach this is via checking where /proc/<pid>/exe
. This file is a symlink that points to the executable file. Thus we can do
user@ubuntu:~$ ls -l /proc/$$/exe
lrwxrwxrwx 1 adminx adminx 0 Apr 4 18:20 /proc/1241/exe -> /bin/bash
user@ubuntu:~$ sh
$ ls -l /proc/$$/exe
lrwxrwxrwx 1 adminx adminx 0 Apr 4 18:20 /proc/1255/exe -> /bin/dash
Either of the two approaches works in 99% of the cases. Of course, there are ways in which they can be subverted. For instance, symlink won't point anywhere if the executable was deleted shortly after the shell started ( and in that case you probably will encounter system issues, since removing /bin/sh
, /bin/dash
, or even /bin/bash
is not recommended - after all a lot of scripts rely on them, especially system-level ones). Command name for shell is usually set as the very first argument in execve()
syscall. This is covered in How does bash know how it is being invoked? , so if you have an application that launches a shell via execve()
, it could give it any name. But these are non-standard and non-typical things, that should be avoided for the sake of consistency and security.
Getting Terminal Information
We can start with the environment variables. Many terminals seem to mask themselves as xterm
-compatible, which is reported by echo $TERM
or echo $COLORTERM
. But then environment variables are not very reliable tool. They can be set and unset. We can do the same thing with PIDs again, except this time we will look at parent PID. As you may remember, terminal is the interface to the shell and often starts the shell itself. Therefore we can find out what process is the parent process of our shell:
$ ps -p $$ -o args,ppid
COMMAND PPID
bash 1234
$ ps -p 1234 -o args
COMMAND
/usr/lib/gnome-terminal/gnome-terminal-server
Let's try with another terminal app, sakura
:
$ ps -p $$ -o args,ppid
COMMAND PPID
/bin/bash 16950
$ ps -p 16950 -o args
COMMAND
sakura
From there we can already see that what started this shell is gnome-terminal
. This method of course works assuming you're working with interactive shell. If we're trying to find out the parent of bash -c '...'
or the shell started via ssh
, for example, PID may very well be from non-terminal application and maybe non-GUI at all.
So if we want to specifically deal with GUI terminal, what we can do is run xprop
, click on the desired window, grep its pid, and find out what's the name of that process matching pid. Or in other words:
$ ps aux | grep $(xprop | awk -F'=' '/PID/ {print $2}')
xieerqi 2124 0.6 1.7 208068 34604 ? Sl 18:47 1:49 gnome-terminal
Additionally, as per specifications , window managers should set WM_CLASS
property. Thus, we can get that from xprop
as well:
$ xprop WM_CLASS
WM_CLASS(STRING) = "sakura", "Sakura"
Of course, this also has its 1% of disadvantages: setting WM_CLASS
properties relies on window manager doing that, and PID is not guaranteed for a window to be accurate ( see What process created this X11 window? ), which may involve complex debugging. And these aren't shortcomings of the methods themselves but of X11 server. However, most stable and well known window managers ( like openbox, Metacity, blackbox ) and most applications are well behaved so we shouldn't expect problems with something like Gnome Terminal or Terminator.
But when it comes to GUI terminal emulators, we don't even need to find a command. We can just use the About
dialog of the window itself. Exception to that rule is xterm
.
Solution 2:
If you want to know the terminal program you are using, use this:
ps -o 'cmd=' -p $(ps -o 'ppid=' -p $$)
Run this just after opening the terminal (shell) without forking any further shell instance.
When you open up the terminal program, it basically spawns a child program, a shell. So the parent of the spawned shell is the terminal itself. In other words, the PPID of the shell is the PID of terminal program.
Here we are finding the parent process ID (PPID) of the shell (bash
) by ps -o 'ppid=' -p $$
, which will be the process ID of terminal program.
Then we are finding the process name from the PID:
$ ps -o 'cmd=' -p $(ps -o 'ppid=' -p $$)
gnome-terminal
It is basically a one liner of:
$ ps -o 'ppid=' -p $$
2268
$ ps -o 'cmd=' -p 2268
gnome-terminal
Solution 3:
The short version (thx @Serg)
cat /etc/alternatives/x-terminal-emulator
The long version
sudo update-alternatives --config x-terminal-emulator
and look for the *
in the output
;)
Example output
There are 7 alternatives which provide `x-terminal-emulator’.
Selection Alternative ———————————————– 1 /usr/bin/xterm 2 /usr/bin/uxterm 3 /usr/bin/koi8rxterm 4 /usr/bin/lxterm *+ 5 /usr/bin/gnome-terminal.wrapper 6 /usr/bin/konsole 7 /usr/bin/xfce4-terminal.wrapper
Press enter to keep the default[*], or type selection number:
Or, thanks to @muru, here is more detailed output
$ update-alternatives --display x-terminal-emulator
x-terminal-emulator - auto mode
link currently points to /usr/bin/gnome-terminal.wrapper
/usr/bin/gnome-terminal.wrapper - priority 40
slave x-terminal-emulator.1.gz: /usr/share/man/man1/gnome-terminal.1.gz
/usr/bin/koi8rxterm - priority 20
slave x-terminal-emulator.1.gz: /usr/share/man/man1/koi8rxterm.1.gz
/usr/bin/lxterm - priority 30
slave x-terminal-emulator.1.gz: /usr/share/man/man1/lxterm.1.gz
/usr/bin/mate-terminal.wrapper - priority 30
slave x-terminal-emulator.1.gz: /usr/share/man/man1/mate-terminal.1.gz
/usr/bin/uxterm - priority 20
slave x-terminal-emulator.1.gz: /usr/share/man/man1/uxterm.1.gz
/usr/bin/xterm - priority 20
slave x-terminal-emulator.1.gz: /usr/share/man/man1/xterm.1.gz
Current 'best' version is '/usr/bin/gnome-terminal.wrapper'.