13

How can I set the window title in Bash? I do know that in Windows Batch it is TITLE.

Nathan
  • 1,266
  • 5
  • 16
  • 34
Deniz Zoeteman
  • 1,081
  • 12
  • 26
  • 48

7 Answers7

14

Here is a nice function to do it:

# Allow the user to set the title.
function title {
   PROMPT_COMMAND="echo -ne \"\033]0;$1 (on $HOSTNAME)\007\""
}

Put that in your ~/.bashrc, then type "title whatever" to set the title. If you want to get rid of the hostname, remove "(on $HOSTNAME)".

Edit: make sure to . ~/.bashrc (aka source ~/.bashrc) before trying, of course.

Source link.

quack quixote
  • 42,186
  • 14
  • 105
  • 129
danben
  • 256
  • 2
  • 4
  • How can i just do everything in the code? I dont want the user to type it in. –  Dec 17 '09 at 17:28
  • You could have your bash code call the title function. –  Dec 17 '09 at 17:33
  • it does not work, when i enter the function stuff, and then enter this `title Hey!`, it does not work. What to do? –  Dec 17 '09 at 17:45
  • 1
    At the risk of repeating myself, did you remember to execute your bashrc script after editing it? It works fine for me. –  Dec 17 '09 at 18:09
  • @YourComputerHelpZ: are you using an xterm, or another kind of console such as Konsole or yakuake ? – raphink Dec 18 '09 at 16:17
  • i am using gnome – Deniz Zoeteman Jan 03 '10 at 19:01
  • Different reference/source link: [xterm Title Bar Manipulations, in Bash Prompt HOWTO](http://www.iitk.ac.in/LDP/HOWTO/Bash-Prompt-HOWTO/xterm-title-bar-manipulations.html) – Tom Hundt Aug 13 '20 at 16:13
  • run `echo -ne \"\033]0;$1 (on $HOSTNAME)\007\"` directly also change title on running – Benyamin Limanto Jul 20 '21 at 04:28
3

I have this VT100 escape sequence defined in .bashrc.

PS1_SET_TITLE='\[\e]0;\u@\h:\w\a\]'

PS1="${PS1_SET_TITLE}" my other prompt components

export PS1

For my home directory it displays alex@host:~, when I change directories, they are updated in window title.

Works with CYGWIN and PuTTY terminal sessions. I usually don't run X, but when I did it worked fine with XTerm.

Read PROMPTING section of bash man page on available switches for PS commands, e.g \u \h \w.

2

Wrote a function title. It supports echo-like escape sequences and -e + -E.

title () {
  echo -ne "\e]0;"
  echo -n "$@"
  echo -ne "\a"
}

I put it in my .bash folder and sourced it from .bash/global.rc.

1

Here is some code to set window title in bash - an improved version, that doesn't remove
previous prompt string (and changes to it are temporary), of another answer (quoted below):

function title {
    export WTITLE=$1
}
PS1_old="$(echo $PS1 | sed -En 's/(.+)\\e](.+)/\1\\\\e]\2/g; s/(.+ )(.+)/\1\\n\2/p')";
_PS1='\[\e]0;$WTITLE: \w\a\]';_PS1+="$PS1_old ";export PS1=$_PS1;

# A command to use in bash (shell) scripts, replaces
# the above function that is for a "~/.bashrc" file:
export WTITLE="[for example \u@\h, insert title here]"

If you are using "mintty" (the default terminal of Cygwin since end 2011),
add the following in .bashrc :

function title {
    export WINDOWTITLE=$1
}    
export PS1='\[\e]0;$WINDOWTITLE:\w\a\]\n\[\e[32m\]\u@\h \[\e[33m\]~\w\[\e[0m\]\n\$'

and reopen your terminal and type "title ThisIsMyTitle"

- quote from this answer.

Edward
  • 115
  • 7
1

If you are using "mintty" (the default terminal of Cygwin since end 2011), add the following in .bashrc :

function title {
   export WINDOWTITLE=$1
}    
export PS1='\[\e]0;$WINDOWTITLE:\w\a\]\n\[\e[32m\]\u@\h \[\e[33m\]~\w\[\e[0m\]\n\$'

and reopen your terminal and type "title ThisIsMyTitle"

metatechbe
  • 159
  • 4
  • 1
    There's an improved version of this, that doesn't remove (previous) prompt string, at https://superuser.com/a/1366261/736409 . – Edward Oct 12 '18 at 15:30
0

I use simple title bash script that lies in PATH:

#!/usr/bin/env bash
echo -ne "\033]0;$@\007"

Usage:

title My favourite window title

Works once, so if any other program changes the title - you need to run the script once again.

pbies
  • 2,757
  • 3
  • 21
  • 23
0

We need more information: The answer will depend on what terminal you're using, not what shell. Is this in an xterm? An rxvt? A cygwin window on windows? Etc.

(danben's answer works for xterms, and probably for rxvt terminals)