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)?
Best Answer
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):So, the PID of my shell's parent is
544
.Get the process associated with that PID and print its command line
The above output will depend on what terminal emulator you are using, I am using
terminator
.Combine everything in a single command
Use that to get the version
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):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
terminator
rxvt
, this one has none of the-V
,-version
or--version
flags so no version info is printed.gnome-terminal
.konsole
lxterminal
xfce4-terminal
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 likexdotool
(installable withsudo apt-get install xdotool
) to get the information instead: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:
This means that getting the version is trivial. For example
Not so for
gnome-terminal
:or
terminator
:So, we can make it a little more complex (there are some bashisms here, this one is not portable):
This works for all cases I tested on.