Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Can one detect what terminal application is being used? I want the actual application, not TERM env in the question.

I'd like to detect iTerm 2 / Terminal.app so I could set OSX specific keyboard mappings, otherwise PC.

(See ALT+arrow moving between words in ZSH and iTerm 2 )

share|improve this question
    
You could see if the application is "running" with "ps -ax | grep iTerm" But I am not exactly sure how you would see if it was being "used" – jds May 23 '12 at 15:43
up vote 0 down vote accepted

You need to SSH forward local environment variables, as explained here:

http://groups.google.com/group/iterm2-discuss/msg/7cc214c487d31bc8

share|improve this answer

I made the following script:

#!/bin/bash

pid=$$ # Current PID
ps -f $$ | head -n 1 # Show the header of ps
while [ $pid -gt 0 ]; do # No more parent when we reach 0 (the kernel)
        ps -f $pid | tail -n +2 # ps current pid and remove header
        pid=$(ps -o ppid $pid|tail -n 1) # Get parent pid
done

It takes the current PID ($$ in bash) and recursively gets the parent PID until we reach 0 (which is the kernel), printing the ps -f output along the way (and a header to start, with ps -f | head -n 1)

Two limitations I can think of:

  1. If run over SSH, the parent will be sshd and not the graphical terminal application.
  2. If executed in a separate script it will print that script too.

However you should be able to grep its output and detect if one of the parent process is iTerm.app or Terminal.app when run locally.

share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .