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.
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 this answer: https://stackoverflow.com/questions/7052683/how-can-i-identify-the-current-terminal-emulator-from-a-bash-script/7053241#7053241
Not every program sets these variables, but echo $TERM_PROGRAM at least works in Terminal.app, iTerm2, and WezTerm. It does not work with Alacritty, but that's pretty typical of their ultra-minimalist feature set.
You need to SSH forward local environment variables, as explained here:
http://groups.google.com/group/iterm2-discuss/msg/7cc214c487d31bc8
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:
sshd and not the graphical terminal application.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.