'How can I identify the current terminal emulator from a bash script?
I've got a script that will open a new tab in the OS X Terminal application and I'm trying to add support for iTerm2. Unfortunately, the methods to open tabs in the two terminal emulators are different.
How could I tell which of the two is being used, or is open, to conditionally run the correct script?
Solution 1:[1]
I'm not sure how to tell iTerm and iTerm2 apart, but check the $TERM_PROGRAM envar. For me (Mac OS X 10.7), it returns Apple_Terminal for Terminal.app, and iTerm.app for iTerm2.
Solution 2:[2]
Working fine on Ubuntu & mint Linux--
ps -o comm= -p "$(($(ps -o ppid= -p "$(($(ps -o sid= -p "$$")))")))"
Solution 3:[3]
I realize this sounds sarcastic, though why didn't they title the function of "x-terminal-emulator" variably "fly-little-birdy"?
I use Linux. Once I perform a "x-terminal-emulator" function call within bash, that terminal is gone & inaccessible to my scripts.
My point is, bash needs more features for full functionality & utility. A feature of "x-terminal-emulator -attach-to-pid=TERMINAL_NAME" would be alright.
I think it's an oversight in functionality & utility. They could "fix it". :/
Solution 4:[4]
You should be able to:
ps -p $$ | tail -1 | awk '{print $NF}'
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | fireshadow52 |
| Solution 2 | Levi_OP |
| Solution 3 | JasonClandestino |
| Solution 4 | sbrichards |
