6

I cannot install any tools like screen or xproc on the box. I dont need to modify the title, I only need to find the window title name.

echo -e "\033]0;[title]\07"; modifies the title I know. But I just want to know the existing title name.

8
  • 1
    does unix.stackexchange.com/a/122870/117549 do it for you? Commented Nov 12, 2018 at 15:27
  • 1
    or possibly unix.stackexchange.com/a/274386/117549 Commented Nov 12, 2018 at 15:28
  • my os does not recognize those as commands Commented Nov 12, 2018 at 16:39
  • 1
    printf '\e[21t' should cause your terminal to return its title in the form \e]title\e\\ . I'll try to wrap that in a script if they don't delete your question until then. Commented Nov 12, 2018 at 16:46
  • I tried the above and got the result below, this is definitely in the right direction, as I was not getting any output before. print "\033[21t" >$(tty) IFS='' read -t 3 -r a output : ^[]l^[\ Commented Nov 13, 2018 at 15:38

2 Answers 2

4

Try the following, but notice that the compatibility is pretty limited. See the notes below.

get_title(){( set -e ss=`stty -g`; trap 'exit 11' INT QUIT TERM; trap 'stty "$ss"' EXIT e=`printf '\033'`; st=`printf '\234'`; t= stty -echo -icanon min 0 time "${2:-2}" printf "${1:-\033[21t}" > "`tty`" while c=`dd bs=1 count=1 2>/dev/null` && [ "$c" ]; do t="$t$c" case "$t" in $e*$e\\|$e*$st) t=${t%$e\\}; t=${t%$st}; printf '%s\n' "${t#$e\][lL]}"; exit 0;; $e*);; *) break;; esac done printf %s "$t"; exit 1 )} 

Example:

$ get_title $ title=`get_title` 

Or, if your script's stdin is not the terminal:

$ title=`get_title </dev/tty` 

The stty + dd kludge tries to make sure that the script won't just block if the terminal doesn't report anything in response to the \e[21t escape. This (or a better) approach could be also used with other control sequences (eg. to get the cursor position).

Notes:

Since the \e[21t escape is considered "insecure", extra configuration is needed in order to make it work:

For xterm: echo '*.vt100.allowWindowOps: true' | xrdb -override

For urxvt: echo 'Rxvt.insecure: true' | xrdb -override

It will not work at all in vte-based terminals like gnome-terminal, mate-terminal, xfce4-terminal, etc, since they report either a fake ("Terminal") or empty title in response.

mlterm doesn't need anything special, but it will crash (!) if the title wasn't set before with \e]2;TITLE\a (that bug was fixed in the current sources).

screen will report its own window title (the one that was set with the -t option or the C-a A command, not the title of the window it's running in.

It's blocked and not supported in tmux.

2
  • 1
    Thank you very much for your help. Much appreciated Commented Dec 3, 2018 at 23:09
  • @Bootham_Deyyam , since it appears that this answer works for you, you should mark it as correct (click the check mark under the votes). In my opinion, you should also up-vote it. This is a great solution. Commented Apr 24, 2020 at 19:11
0

For example, getting the title of konsole window current interactive bash instance is running in:

#!/bin/bash get_title() { local pid=$$ local pids=`pstree -lpsT $pid | tr '-' '\n' | tac | xargs | grep -oE '[0-9]+'` for pid in $pids do local wid=`xdotool search --pid $pid` if [ ! -z "${wid}" ] then echo `xdotool getwindowname $wid` exit fi done } get_title 
1
  • but wrong title if run in ssh.. Commented Jul 30, 2024 at 1:31

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.