Canonical way for a shell to detect if it's running in iTerm2

1,255 views
Skip to first unread message

Aaron Meurer

unread,
Oct 26, 2015, 2:09:21 PM10/26/15
to iterm2-...@googlegroups.com
Is there a canonical way for a shell to detect if it's running in
iTerm2? I noticed that ITERM_PROFILE and ITERM_SESSION_ID environment
variables are set? Are there better ways?

This is for the purposes of automatically enabling shell integration,
so something that only works for newer versions is also fine (and
something that also gives the iTerm2 version would be great).

Aaron Meurer

TJ Luoma

unread,
Oct 26, 2015, 2:36:35 PM10/26/15
to iterm2-...@googlegroups.com


I use

if [ "$TERM_PROGRAM" = "iTerm.app" ]
then

Which also lets me check for Apple's

Apple_Terminal

Or

Prompt_2

Etc
> --
> You received this message because you are subscribed to the Google Groups "iterm2-discuss" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to iterm2-discus...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

George Nachman

unread,
Oct 26, 2015, 3:04:27 PM10/26/15
to iterm2-discuss
You can't count on env variables getting passed through SSH. 2.9.x will respond to the escape sequence found here (https://github.com/gnachman/iTerm2/blob/master/tests/report_name_tn) with a code that identifies the terminal uniquely. Unfortunately, most other terminals will simply respond with silence, so it's not terribly reliable.

There isn't a way to get the version number. I could easily add another sequence like the tn one above, but I'm not sure how useful it would be.

George Nachman

unread,
Mar 4, 2016, 2:11:01 AM3/4/16
to iterm2-...@googlegroups.com
I think I figured out how to tell if your terminal is iTerm2. It needs testing before wide deployment on different platforms.

The problem has always been that environment variables don't get passed through and almost all terminals don't support an escape sequence to report version number. Commit cc8436b adds support for a new escape sequence that reports the terminal name and version number that should be ignored by all other terminals. The trick is you can't just ask for the version and then read it back because that'll hang indefinitely if the terminal doesn't support the new sequence. Instead, you send the custom sequence and then a standard "device status report" which every terminal supports. Then you read back one response, and if it's a DSR response you quit, knowing it's not iTerm2. Otherwise, read both responses.

Commit 95c439f44a77e2419c73e2a44e7f0b423ed37368 adds a shell script that tests if the current terminal is iTerm2 and optionally checks if its version number is greater than or equal to one you specify.

It will only work on versions of iTerm2 that support this new escape sequence, starting with tomorrow's nightly build (2.9.20160304).

tl;dr: You can tell if a terminal is iTerm2 by using this script:  https://raw.githubusercontent.com/gnachman/iTerm2/master/tests/isiterm2.sh

Reply all
Reply to author
Forward
0 new messages