3

I'm trying to make a bash script that behaves differently based on the terminal program that someone is using (Putty, mobaxterm, etc). Is there any way to retrieve this kind of information from a bash script?

I was searching around online but I was not able to find anything (or I'm just wording it incorrectly, which could be a distinct possibility).

thank you

lacrosse1991
  • 2,972
  • 7
  • 38
  • 47
  • 3
    What do you want to do differently for different terminals? – svsd Oct 03 '15 at 05:10
  • 1
    Yes, tell us what you want to do. Sounds a lot like an XY-Problem - http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem . – sleske Oct 03 '15 at 09:13
  • There's no reliable way to do this. A related question was asked in [Discovering remote Terminal for Terminal Escape Codes? (DECDHL in this case)](http://stackoverflow.com/questions/32019235/discovering-remote-terminal-for-terminal-escape-codes-decdhl-in-this-case). Agreeing that OP should identify the actual problem to be solved is a first step toward answering the question. – Thomas Dickey Oct 03 '15 at 16:50

2 Answers2

1

You need to understand that these are terminal emulators. There is a simple, crude identification function for (most?) modern line terminals (now that's an oxymoron!) but it will return something like vt102 or xterm i.e. whatever the emulator is emulating; not the identity of the program performing this emulation. Incidentally, this is usually used when the session is initiated, and reflected in the value of the $TERM environment variable.

tripleee
  • 175,061
  • 34
  • 275
  • 318
  • ...and as a rule, the information is incorrect (VTE-based terminals claim functionality which they do not implement, other terminals such as Konsole under-report the feature). Likewise, `TERM` is largely meaningless. – Thomas Dickey Oct 03 '15 at 16:48
  • One notable exception to ENQ is the Sun console. It lacks the feature altogether. – Thomas Dickey Oct 03 '15 at 17:08
  • For other examples, the source-code for [qterm](http://hpux.connect.org.uk/hppd/hpux/Misc/qterm-6.0.3/man.html) would be useful to read. – Thomas Dickey Oct 03 '15 at 17:37
  • On the other hand, `ENQ` is used rarely with Linux. It used to be useful in combination with `tset`, but I've not seen any (non-museum) uses within the past 10-15 years. – Thomas Dickey Oct 03 '15 at 17:45
0

The common way to do this is the environment variable TERM. Try

echo $TERM

That should output the terminal emulator's type, indicating its capabilities. Often, though not always, the value of TERM will be the name of the terminal emulator. For example, xterm may set TERM to xterm, or to xterm-color, depending on configuration.

Note that the user can change this variable, so it may contain something completely different.

That said, if you want to do fancy things with the terminal from a script, you don't need to build support for different terminals by hand. There are various libraries that offer all the usual functions (clear terminal, resize window, change font etc.). The most popular one is terminfo; there are various packages that build on terminfo.

sleske
  • 81,358
  • 34
  • 189
  • 227
  • 1
    `TERM` is not the terminal emulator's name, but instead is the name for a set of features which are supposed to match a given terminal. – Thomas Dickey Oct 03 '15 at 16:48
  • @ThomasDickey: Yes, thanks for pointing it out (though often the "name for the set of features" happens to be the terminal name, e.g. "xterm"). Edited answer. – sleske Oct 03 '15 at 17:28
  • There are a dozen or more entries for xterm (see [link](http://invisible-island.net/xterm/terminfo.html)), which all differ (according to given configurations). – Thomas Dickey Oct 03 '15 at 17:31
  • @ThomasDickey: Thanks for the list. And I just realized who you are - you should probably have answered yourself, you certainly know a lot more about terminfo and xterm than I do :-). – sleske Oct 03 '15 at 18:39
  • It's awkward, when there are two answers asserting it's possible, and the OP has not clarified what's wanted. I may followup if OP provides useful information. – Thomas Dickey Oct 03 '15 at 18:53