change terminal title

6,567

Solution 1

In Bash, I use the following trap command in the last line of my .bashrc to echo all commands to the titlebar.

# trap commands and echo them to xterm titlebar. Must be last line.
trap 'echo -ne "\033]0;$BASH_COMMAND $USER@${HOSTNAME}>$(pwd)\007"' DEBUG

So if you execute sleep 10, you will see your titlebar change to sleep 10 for the duration of the command, after which it will change back to user@host>pwd.

Solution 2

Most terminals recognize a certain escape sequence as an attempt to change the title, so this will work:

$ alias foo-with-title='echo -ne "\033]0;foo\007"; foo'
Share:
6,567
JoelFan
Author by

JoelFan

Updated on September 18, 2022

Comments

  • JoelFan
    JoelFan over 1 year

    I want to make a shell alias that starts a certain program and also changes the title of the terminal to the name of the program. How can I do that?

    In case it matters, I am running the terminal on Ubuntu and connecting to a Debian server via ssh.

  • Gilles 'SO- stop being evil'
    Gilles 'SO- stop being evil' about 13 years
  • enzotib
    enzotib about 12 years
    Where is this documented?
  • oneindelijk
    oneindelijk almost 5 years
    This works in a bare shell. Using tmux/byobu this doesn't seem to work. I also tried bash -c 'echo -ne "\033]0;foo\007"'