Results 1 to 9 of 9

Thread: Cross-terminal-emulator way to set terminal title in bash/ksh script?

  1. #1
    Join Date
    Jun 2016
    Beans
    2,831
    Distro
    Xubuntu

    Cross-terminal-emulator way to set terminal title in bash/ksh script?

    This code works in Konsole -
    Code:
    echo -e -n "\033]30;Custom Title\007"
    But it doesn't work in xfce4-terminal, or any other terminal emulator that I've tried? And I can't find a code that does?

    How to have a bash/ksh script set terminal window title in a way that works across different terminal emulators?
    Xubuntu 22.04, ArchLinux ♦ System76 hardware, virt-manager/KVM, VirtualBox
    When your questions are resolved to your satisfaction, please use Thread Tools > "Mark this thread as solved..."

  2. #2
    Join Date
    Apr 2020
    Beans
    70

    Re: Cross-terminal-emulator way to set terminal title in bash/ksh script?

    https://unix.stackexchange.com/quest...a-shell-script

    echo -e '\033]2;SomeTitle\007'


    --Works in OSX High Sierra with builtin Terminal for me. Doesn't work on Ubuntu 20.04 with gnome-terminal. xfce4-terminal has a "set title" menu option/hotkey but that escape-code line doesn't work there either.


    --You might try installing the ' wmctrl ' package and look at the man page, search for ' title '

    --TBH (this might be outside the box for you) I never worry about my terminal title, I use GNU ' screen ' and put my stuff in the .screenrc init file. That actually could be a more portable solution for you.

    $ cat .screenrc
    hardstatus alwayslastline
    hardstatus string '%{= kG}[ %{G}%H %{g}][%= %{=kw}%?%-Lw%?%{r}(%{W}%n*%f %t%?(%u)%?%{r})%{w}%?%+Lw%?%?%= %{g}][%{c}%Y-%m-%d %{W}%c %{g}]'
    # cyan for non-root date = readability xxx 2020.0605
    # color ref: https://www.gnu.org/software/screen/...String-Escapes
    # Default screens

    screen -t shell1 0 /bin/bash
    screen -t shell2 1 /bin/bash
    screen -t shell3 2 /bin/bash


    # switch back to 1st window
    select 0

  3. #3
    Join Date
    Apr 2020
    Beans
    70

    Re: Cross-terminal-emulator way to set terminal title in bash/ksh script?

    --And also, don't forget you can always spawn another ' xterm -name blah & ' and disown on the original terminal, if you don't really need to change it on the fly.

  4. #4
    Join Date
    Jun 2016
    Beans
    2,831
    Distro
    Xubuntu

    Re: Cross-terminal-emulator way to set terminal title in bash/ksh script?

    Quote Originally Posted by kneutron View Post
    --You might try installing the ' wmctrl ' package and look at the man page, search for ' title '
    Thanks kneutron, that almost solves this problem. I can make it work if I run the script using a .desktop file with Terminal=true, which is what I need now. But there are two issues:

    1) When I first open terminal then run the script from terminal, I haven't been able to get it to work, because I'm finding the terminal window ID based on looking for $PPID in wmctrl -l -p output. I don't know how to get the parent PID of the parent PID, recursing until hitting a PID in the wmctrl list. Is this a reasonable approach? If so, how to "walk up the PID tree" to get the desired PID?

    2) If I have multiple terminal windows open from the same terminal process, how to pick out only the terminal window running my script, to avoid changing title on unrelated terminal windows?
    Xubuntu 22.04, ArchLinux ♦ System76 hardware, virt-manager/KVM, VirtualBox
    When your questions are resolved to your satisfaction, please use Thread Tools > "Mark this thread as solved..."

  5. #5
    Join Date
    Apr 2020
    Beans
    70

    Re: Cross-terminal-emulator way to set terminal title in bash/ksh script?

    > I don't know how to get the parent PID of the parent PID, recursing until hitting a PID in the wmctrl list. Is this a reasonable approach? If so, how to "walk up the PID tree" to get the desired PID?

    --Try ' pstree ', you might have to awk the output

    >
    2) If I have multiple terminal windows open from the same terminal process, how to pick out only the terminal window running my script, to avoid changing title on unrelated terminal windows?

    --Remember my ' xterm -name ' suggestion, you could launch the script with xterm -e and not have to worry about changing the title on the fly
    Last edited by kneutron; August 7th, 2020 at 03:43 PM.

  6. #6
    Join Date
    Jun 2016
    Beans
    2,831
    Distro
    Xubuntu

    Re: Cross-terminal-emulator way to set terminal title in bash/ksh script?

    Thanks for your help kneutron! I now got something good enough for me.

    To sum up:

    1) Take advantage of KONSOLE_VERSION environment variable to detect Konsole. If using Konsole, just use the code in the OP. Simple, straightforward, and nice.

    2) Some terminal emulators (e.g. xfce4-terminal, mate-terminal, xterm, and also Konsole) set an environment variable WINDOWID , this can be converted to hex and used with wmctrl. Note that if wmctrl -l lists leading zeros for a window ID, it seems to need those passed back in when selecting a window by ID.

    3) For terminal emulators that make a mess of WINDOWID (like QTerminal which always sets it to 0) or don't do WINDOWID at all, I'm stuck hoping there's one terminal window per terminal process and fall back to using
    Code:
    pstree -p -T -s "$PPID"
    and
    Code:
    wmctrl -l -p
    to make best guess of the window ID.


    Note that (2) and (3) won't work if done too early, you might need to add some sleep in your code or otherwise find a way to wait for the terminal to be fully open.

    Unfortunately with gnome-terminal none of these methods work reliably enough with gnome-terminal, but I primarily use Konsole and secondarily xfce4-terminal so this is fine for me for now.
    Xubuntu 22.04, ArchLinux ♦ System76 hardware, virt-manager/KVM, VirtualBox
    When your questions are resolved to your satisfaction, please use Thread Tools > "Mark this thread as solved..."

  7. #7
    Join Date
    Oct 2005
    Location
    Lab, Slovakia
    Beans
    10,792

    Re: Cross-terminal-emulator way to set terminal title in bash/ksh script?

    Note that there are various ways to prettify scripts. I prefer Zenity:
    https://www.aeronetworks.ca/2015/09/...-dialogue.html

  8. #8
    Join Date
    Jun 2016
    Beans
    2,831
    Distro
    Xubuntu

    Re: Cross-terminal-emulator way to set terminal title in bash/ksh script?

    Quote Originally Posted by HermanAB View Post
    Note that there are various ways to prettify scripts. I prefer Zenity:
    Zenity is great for giving a script some GUI, I have some convenience scripts that use it similar to how that blog link describes. But in this case adding GUI elements would not do the job.
    Xubuntu 22.04, ArchLinux ♦ System76 hardware, virt-manager/KVM, VirtualBox
    When your questions are resolved to your satisfaction, please use Thread Tools > "Mark this thread as solved..."

  9. #9
    Join Date
    Jun 2016
    Beans
    2,831
    Distro
    Xubuntu

    Re: Cross-terminal-emulator way to set terminal title in bash/ksh script?

    Quote Originally Posted by halogen2 View Post
    But it doesn't work in xfce4-terminal, or any other terminal emulator that I've tried? And I can't find a code that does?
    Turns out I was at least in part blocked by my .bashrc being a modification of standard Ubuntu bashrc, which contains this code -
    Code:
    # If this is an xterm set the title to user@host:dir
    case "$TERM" in
    xterm*|rxvt*)
        PS1="\[\e]0;${debian_chroot:+($debian_chroot)}\u@\h: \w\a\]$PS1"
        ;;
    *)
        ;;
    esac
    The following code does momentarily change the terminal title in xfce4-terminal and mate-terminal -
    Code:
    echo -e -n '\033]0;Custom Title\007';sleep 5
    I like the effect of the bashrc code, so I will still stick with the wmctrl-based solution.
    Xubuntu 22.04, ArchLinux ♦ System76 hardware, virt-manager/KVM, VirtualBox
    When your questions are resolved to your satisfaction, please use Thread Tools > "Mark this thread as solved..."

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •