Discussion:
Get terminal/window title
(too old to reply)
Janis Papanagnou
2021-04-20 01:18:32 UTC
Permalink
With the standard shell terminal that I use on Ubuntu I can set the
window title. E.g. by using the GUI menu (Terminal-> Set Title...),
or by setting it with a shell command, e.g. with something like

echo -ne "\033]0;SOME TITLE HERE\007"

Is there an equally simple way to _interrogate_ the window title by
a shell command?

Janis
Kaz Kylheku
2021-04-20 01:59:01 UTC
Permalink
Post by Janis Papanagnou
With the standard shell terminal that I use on Ubuntu I can set the
window title. E.g. by using the GUI menu (Terminal-> Set Title...),
or by setting it with a shell command, e.g. with something like
echo -ne "\033]0;SOME TITLE HERE\007"
Is there an equally simple way to _interrogate_ the window title by
a shell command?
Yes and no. See here: https://unix.stackexchange.com/a/482818/16369

Yes and no. There is a ESC[21t sequence. It only works on xterm,
and a couple other terminals. It's considered a security hole, so
requires configuration to allow it. Some emulators give a canned answer
which is not actually the dynamic title.
--
TXR Programming Language: http://nongnu.org/txr
Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal
Kenny McCormack
2021-04-20 05:18:40 UTC
Permalink
Post by Janis Papanagnou
With the standard shell terminal that I use on Ubuntu I can set the
window title. E.g. by using the GUI menu (Terminal-> Set Title...),
or by setting it with a shell command, e.g. with something like
echo -ne "\033]0;SOME TITLE HERE\007"
Is there an equally simple way to _interrogate_ the window title by
a shell command?
xdotool getactivewindow getwindowname

should do it (at least as long as the calling window is the active window.

In fact, it is kind of a weakness in xdotool, that there is no obvious way
to get information about the calling process's window; using
getactivewindow is a kludge, but it is likely to work in most cases.

Thinking about this some more, I suppose what you could do is:

# At the beginning...
echo -ne "\033]0;SOME TITLE HERE\007"
myWinId=$(xdotool search --only --sync --name "SOME TITLE HERE")

# Then later on...
# set it to whatever you really want it to...
echo -ne "\033]0;whatever whatever probably dynamic\007"

# Then later on...
echo "My window title is: $(xdotool getwindowname $myWinId)"
--
I am not a troll.
Rick C. Hodgin
I am not a crook.
Rick M. Nixon
Richard Harnden
2021-04-20 10:07:52 UTC
Permalink
Post by Janis Papanagnou
With the standard shell terminal that I use on Ubuntu I can set the
window title. E.g. by using the GUI menu (Terminal-> Set Title...),
or by setting it with a shell command, e.g. with something like
echo -ne "\033]0;SOME TITLE HERE\007"
Is there an equally simple way to _interrogate_ the window title by
a shell command?
Janis
If you set the title as part of PS1, then you can query that ... ?
Grant Taylor
2021-04-20 15:25:48 UTC
Permalink
Post by Richard Harnden
If you set the title as part of PS1, then you can query that ... ?
That's only reliable while sitting at the prompt. (If even then.)

All of my systems change the title to be the command as part of a
pre-command function. So reading $PS1 as part of the command, e.g. a
script, will return something other than the current window title.
--
Grant. . . .
unix || die
Michael F. Stemper
2021-04-20 17:22:50 UTC
Permalink
Post by Richard Harnden
Post by Janis Papanagnou
With the standard shell terminal that I use on Ubuntu I can set the
window title. E.g. by using the GUI menu (Terminal-> Set Title...),
or by setting it with a shell command, e.g. with something like
   echo -ne "\033]0;SOME TITLE HERE\007"
Is there an equally simple way to _interrogate_ the window title by
a shell command?
If you set the title as part of PS1, then you can query that ... ?
As long as you don't use any variables. For instance,

$ echo $PS1
\[\e]0;\u@\h: \w\a\]\u@\h$
$

I suppose that there is a definition for all of this that your program
could use for parsing it out, but that's likely to be error-prone.
--
Michael F. Stemper
Galatians 3:28
Loading...