Узнать имя терминала линукс

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

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.

Thanks but first problem with rxvt -> window 31457282 has no pid associated with it (tested on Lubuntu)

7 Answers 7

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 to ps ( -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

 $ 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=) --version terminator 0.97 
 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/') ;; *lxterminal*) found=1 echo "lxterminal " $(dpkg -l lxterminal | awk '/^ii/') ;; rxvt*) found=1 echo "rxvt " $(dpkg -l rxvt | awk '/^ii/') ;; ## 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/') > 

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:

 $ which_term terminator 0.97 
 $ which_term gnome-terminal 3.10.1-1 
 $ which_term Qt: 4.8.6 KDE Development Platform: 4.11.3 Konsole: 2.11.3 
 $ which_term lxterminal 0.1.11-4 
 $ 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 and Nick Schermer . Please report bugs to . 

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:

Читайте также:  010 editor linux install

This means that getting the version is trivial. For example

$ dpkg -l $(which_term) | awk '/^ii/' 0.1.11-4 
$ which_term /usr/lib/gnome-terminal/gnome-terminal-server 
$ 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/') ;; ## The special case of gnome-terminal *gnome-terminal-server* ) term="gnome-terminal" ;; ## For other cases, just take the 1st ## field of $term * ) term=$ ;; esac version=$(dpkg -l "$term" | awk '/^ii/') echo "$term $version" > 

This works for all cases I tested on.

Источник

Which terminal type am I using?

But if I use the dropdown menu «help» > «about» then it says gnome terminal 3.4.1.1 . Does this mean I am using just gnome-terminal? Or just xterm? Or is gnome-terminal an extension of xterm? I’m confused.

4 Answers 4

What is $TERM for?

The $TERM variable is for use by applications to take advantage of capabilities of that terminal.

For example, if a program wants to display colored text, it must first find out if the terminal you’re using supports colored text, and then if it does, how to do colored text.

The way this works is that the system keeps a library of known terminals and their capabilities. On most systems this is in /usr/share/terminfo (there’s also termcap, but it’s legacy not used much any more).

So let’s say you have a program that wants to display red text. It basically makes a call to the terminfo library that says «give me the sequence of bytes I have to send for red text for the xterm terminal«. Then it just takes those bytes and prints them out.
You can try this yourself by doing tput setf 4; echo hi . This will get the setf terminfo capability and pass it a parameter of 4 , which is the color you want.

Why gnome terminal lies about itself:

Now let’s say you have some shiny new terminal emulator that was just released, and the system’s terminfo library doesn’t have a definition for it yet. When your application goes to look up how to do something, it will fail because the terminal isn’t known.

The way your terminal gets around this is by lying about who it is. So your gnome terminal is saying «I’m xterm«.

Xterm is a very basic terminal that has been around since the dawn of X11, and thus most terminal emulators support what it supports. So by gnome terminal saying it’s an xterm, it’s more likely to have a definition in the terminfo library.

The downside to lying about your terminal type is that the terminal might actually support a lot more than xterm does (for example, many new terminals support 256 colors, while older terminals only supported 16). So you have a tradeoff, get more features, or have more compatibility. Most terminals will opt for more compatibility, and thus choose to advertise themselves as xterm .

If you want to override this, many terminals will offer some way of configuring the behavior. But you can also just do export TERM=gnome-terminal .

The TERM environment variable indicates the terminal type, not the terminal application. TERM has a specific purpose: it tells applications running in that terminal how to interact with the terminal.

Читайте также:  Linux mint смена hostname

Applications interact with terminals by writing escape sequences — sequences of characters that include nonprintable characters and have effects such as moving the cursor, erasing part of the screen, changing the current color, etc. In the old days, different brands of physical terminals had different sets of escape sequences. Therefore the operating system maintains a database of terminal types and their characteristics. The traditional database is termcap (“TERMinal CAPabilities”); many modern systems and applications have switched to terminfo. Both databases are indexed by the name of the terminal type, and applications query them using the terminal type name from the TERM environment variables.

Nowadays, most terminals use a standard set of escape sequences with a few common sets of extensions, so you won’t see many different values of TERM . Most GUI terminal emulators are compatible with xterm, the traditional X terminal (which is still used and maintained).

Terminal emulators that differ from xterm may add their own entry to the terminal database under their own name. However this doesn’t mesh well with remote shells. A program running on machine A but which is displaying on machine B, for example because it was launched through ssh from B to A, needs to query the terminal database on machine A. Remote login methods such as ssh carry over the TERM environment variable, but this is helpful only when B’s terminal database also has an entry for the value user on A. Therefore many terminal emulators stick to TERM=xterm which is pretty much universally known.

The differentiation between terminals by and large does not come from the way applications interact with them, but by the way the terminals interact with the user and fit in their environment. For example, Gnome Terminal looks good on Gnome and provides tabs and other niceties; Konsole looks good on KDE and provides tabs and other niceties; urxvt has a small memory requirement; Console2 runs on Windows; screen and tmux provide sessions that can be attached to different parent terminals; and so on. Since none of these features make a difference to applications running in the terminal, most terminal emulators use TERM=xterm .

To find out what terminal a shell is running in (assuming the shell is running directly in a terminal), look at the parent of the shell:

Источник

How to get the current terminal name?

In the above example, I need to fetch pts/2 which is probably the name for the current terminal that executed those commands.

4 Answers 4

Now I have to enter 30 characters where 3 would have been enough. 🙂

@tripleee If you know the result getting it usually seems easy. But this was a really hard one. I should upvote the question in order to kick him above the 125 rep points border so the he can offer the deserved bounty for my great effort.

Your answer got me wondering where tty gets the name of the PTY or TTY. I ran strace -o spork tty to find out that it does a readlink on a /proc file: readlink(«/proc/self/fd/0», «/dev/pts/5». 4095) = 10 It’s great that /proc can be used to do such nifty things, but what did earlier Unixes, without a flexible /proc filesystem, do to get tty name?

You could simply explain what that command, is, what it does and what else you can do with it and whoooosh, it would be a better answer.

@Bobby Every answer can be made better. The questioner got his problem solved after two minutes; not so bad IMHO. Seriously, I have no idea what else you can do with tty . What it does is exactly what the question is about. I don’t even get the difference between «what that command is» and «what it does». What «is» a command? But, hey, I don’t want upvotes for nothing. If you write a clearly better answer (which mine may have prevented) then I will give a 50 rep bounty for that (as soon as it’s possible i.e. in two days).

Читайте также:  Intel ax210 drivers linux

Re: «You could simply explain what that command, is, what it does and what else you can do with it and whoooosh, it would be a better answer. – Bobby»

The Unix name of the current terminal (or console, as we oldsters use to sometimes also call it) is: /dev/tty which, can be used to easily create a new multi-line file from the command prompt thusly: cp /dev/tty README.md (hitting then puts the cursor on a new blank line where you can enter text, hit return again, enter a second line, etcetera. When finished entering lines, do a control-d which causes the cp command to exit and you will have made a multi-line file with a single command).

The «name» tty is derived from TeleType (the original terminal Device) which also explains why it is in the /dev dir.

Re: «You could simply explain what that command, is, what it does and what else you can do with it and whoooosh, it would be a better answer. – Bobby»

OR we could really go crazy, and say «go look at the source code for more information.» So I did.

To actually get the name in tty.c , an auxilliary function ttyname is called on STDIN_FILENO . Both of these are defined in unistd.h (for proof, run grep «STDIN_FILENO\|ttyname» /usr/include/unistd.h ), which is pulled into tty.c through #include «system.h» (go here to see system.h ).

Now ttyname is an external dependency defined in glib_c/sysdeps/posix/ttyname.c . It, in turn, fstats file descriptor STDIN_FILENO ( 0 ), and uses the function gettyname to actually retrieve a pointer to the name of the tty. fstat ends up leading to an INLINE_SYSCALL in fxstat.c , which ends up invoking an internal_syscall2 , and at this point I’m outta luck. I don’t really know what that does.

But, I believe it invokes the stat functionality on fd 0 . It does all this, making sure not to stop at «stdin» or something like that. Either way, you can achieve the same behavior by looking at

stat /dev/stdin # -> /proc/self/fd/0 stat /proc/self/fd/0 # -> /dev/pts/

I’m trying to get to the bottom of it all, but I won’t be as detailed as above. So far I’ve deduced that the syscall indirection ends up at a function called vfs_fstat which then calls the function fdget_raw which immediately calls __fget_light . This uses a macro current which returns the result of get_current defined here:

static __always_inline struct task_struct *get_current(void)

Anyways, the task_struct has an entry files of type files_struct , which I guess keeps the list of files relevant to the current task. This is an array, and fd is an index into the array. To wrap all this up, we just need to find out what fd 0 really means in reference to this array (i.e. when a task is created, what actually gets put there). Then, we just need to see in the source if /proc/self accurately reflects that array, then we can be sure that what we think happens actually happens.

Источник

Оцените статью
Adblock
detector