Under Construction

Headline Terminal

In most cases users work under UNIX/AIX with a virtual terminal, which is made available by a graphical application such as xterm, gnome-terminal or similar. These graphical applications usually provide escape sequences that can be used to access the title bar. Many users take advantage of this to display information such as username, hostname, and current working directory there.

The terminal type is stored in the environment variable TERM. For xterm and similar applications this is typically the value xterm or xterm-256color or similar. For these terminal emulators, the title bar can be accessed using the following escape sequence:

ESC]0; string BEL

The character string between “ESC]0;” and “BEL” is then displayed in the headline. For example, the command “echo –ne” can be used for this:

[user01@aixe01 ~]$ echo -ne "\033]0;TITLE\007"
[user01@aixe01 ~]$

Note: The string “\033” is the representation for the character ESC and “\007” is the replacement representation for the character BEL. The “-e” option allows bash to interpret such characters.

The character string “TITLE” appears in the headline of the xterm (or similar application).

For example, to display user, computer and current directory in the form “user@host:directory“, the following command can be used:

[user01@aixe01 ~]$ echo -ne "\033]0;${USER}@${HOSTNAME}:${PWD}\007"
[user01@aixe01 ~]$

However, if you now change the directory, the path information in the title bar no longer matches the current position in the file system. The headline is not automatically updated.

However, this is very easy to fix. Bash offers the variable PROMPT_COMMAND, which allows you to store a command that is always executed before bash issues the shell prompt. We store the above command in this variable:

[user01@aixe01 ~]$ PROMPT_COMMAND='echo -ne "\033]0;${USER}@${HOSTNAME}:${PWD}\007"'
[user01@aixe01 ~]$

Note: In order to prevent the shell variables from being evaluated before they are assigned, the entire command has been enclosed in single apostrophes. The variables are only expanded when the PROMPT_COMMAND is executed.

Now the displayed path changes correctly when changing to another directory.

The PROMPT_COMMAND variable should of course be set in one of the bash startup files. We use ~/.bashrc again:

[user01@aixe01 ~]$ cat ~/.bashrc
...
case $TERM in
xterm*) PROMPT_COMMAND='echo -ne "\033]0;${USER}@${HOSTNAME}:${PWD}\007"' ;;
esac
...
[user01@aixe01 ~]$

To ensure that the PROMPT_COMMAND is only set if the terminal is an xterm or similar program, a case statement is used with a label of xterm*.

If the PROMPT_COMMAND is more complex than a simple echo as above, then the definition of a function is recommended:

[user01@aixe01 ~]$ cat ~/.bashrc
...
function __xterm_prompt_command
{
        echo -ne "\033]0;${USER}@${HOSTNAME}:${PWD}\007"
}

case $TERM in
xterm*) PROMPT_COMMAND=__xterm_prompt_command ;;
esac
...
[user01@aixe01 ~]$

On some systems, the window manager sets part of the title. The computer name and/or user name and computer name are then often also written in the title line. If you then log on to another computer via SSH and set the title bar as above, the window manager will also display the original computer name (or more). Two computer names then appear, which can sometimes be a bit annoying. By inserting a few spaces you can at least visually separate them from each other.