I have seen many questions about how to set the tab (or window) name, but how to get it? I have setup terminal to open 8 tabs, each tab having its own name (dev1, dev2, ... dev8) Is there any way for a shell script to identify in which tab it is (read the tab's name?)
If all else fails, then AppleScript to the rescue:
title=`osascript -e 'tell application "Terminal" to get name of front window'` echo $title
Beware though when using
of front window when using multiple windows. This will print the wrong results when switching to another tab in 5 seconds:
sleep 5; osascript -e 'tell application "Terminal" to get name of front window'
(So I hope someone can give you an easier way.)
There is a control sequence for requesting the window title, which corresponds to the sequence for setting the title; however, it is not supported by Terminal, and it is disabled by default in XTerm (and I assume other terminal emulators) for security/privacy reasons.
The XTerm documentation for the control sequences to set/get the window title:
Using application scripting as in Arjan’s answer works, but only when the remote program is on the same computer, and, unlike the control sequence, scripting has security rules, like requiring authentication if the sender and receiver processes don’t have the same owner/user.