Posted: . At: 9:58 AM. This was 9 months ago. Post ID: 18372
Page permalink. WordPress uses cookies, or tiny pieces of information stored on your computer, to verify who you are. There are cookies for logged in users and for commenters.
These cookies expire two weeks after they are set.


Set the terminal title easily on Linux using a simple variable.


Setting the terminal title is very easy on Linux. This can further customize your Linux experience.

The PROMPT_COMMAND variable is the way to do this.

PROMPT_COMMAND is an environment variable in Bash that contains a command to be executed before the primary prompt is displayed. The value assigned to PROMPT_COMMAND is a command enclosed in single quotes. Inside the command:

  • \033]0; is an escape sequence that starts the process of changing the terminal title.
  • $(whoami) is a command substitution that fetches the current username.
  • @ is a literal “@” symbol.
  • $(hostname -s) is a command substitution that fetches the short hostname of the machine.
  • \007 is an escape sequence that terminates the process of changing the terminal title.

So, when you execute this code, every time your prompt is displayed, the terminal title will be updated to show your username and the short hostname of the machine.

To test this code in the terminal, use export to set the variable right away.

(jcartwright@localhost) 192.168.1.5 ~  $ export PROMPT_COMMAND='echo -ne "\033]0;$(whoami)@$(hostname -s)\007"'

To print the machine`s IP address as well, use this example.

(jcartwright@localhost) 192.168.1.5 ~  $ PROMPT_COMMAND='echo -ne "\033]0;$(whoami)@$(hostname -s) - $(hostname -I | cut -d " " -f 1)\007"'

In this modified command, I’ve added the hostname -I command to retrieve the list of IP addresses associated with the machine, and then I’ve used cut to extract the first IP address from the list (assuming you’re interested in the first IP address only).

So, the terminal title will now display your username, machine hostname, and the first IP address associated with the machine.

bash supplies a variable $PROMPT_COMMAND which contains a command to execute before the prompt.
This example sets the title to username@hostname: directory:
PROMPT_COMMAND=’echo −ne “\033]0;${USER}@${HOSTNAME}: ${PWD}\007″‘

Where \033 is the character code for ESC, and \007 for BEL. Note that the quoting is important here: variables are expanded in “…”, and not expanded in ‘…’. So $PROMPT_COMMAND is set to an unexpanded value, but the variables inside “…” are expanded when $PROMPT_COMMAND is used.

However, $PWD produces the full directory path. If we want to use the ~ shorthand we need to embed the escape string in the prompt, which allows us to take advantage of the following prompt expansions provided by the shell:

\u expands to $USERNAME
\h expands to hostname up to first '.'
\w expands to directory, replacing $HOME with '~'
\$ expands to '$' for normal users, '#' for root
\[...\] embeds a sequence of non−printing characters

Below is how to set the terminal title with the C shell.

switch ($TERM)
    case "xterm*":
        set host=`hostname`
        alias cd 'cd \!*; echo −n "^[]0;${user}@${host}: ${cwd}^Gcsh% "'
        breaksw
    default:
        set prompt='csh% '
        breaksw
endsw

And the below example is for the ksh shell on Linux and UNIX.

case $TERM in
    xterm*)
        HOST=`hostname`
        PS1='^[]0;${USER}@${HOST}: ${PWD}^Gksh$ '
        ;;
    *)
        PS1='ksh$ '
        ;;
esac

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.