1. Overview
Sometimes, we might want to change the title of our current terminal emulator tab in the Linux command line interface (CLI). For example, this task is essential in different scenarios, such as adding information about the current session directly into the tab title or efficiently managing multiple terminal tabs in general.
In this tutorial, we’ll check out methods for changing the title of the current terminal on a Ubuntu 22.04 GNOME desktop.
Specifically, we use the PS1 and the PROMPT_COMMAND environment variables and edit the ~./bashrc file as well.
2. Problem Statement
In Linux, the default title of a terminal tab is often determined by the terminal emulator and the shell in use. Moreover, it often includes information such as the username, the machine’s hostname, and the current working directory.
For demonstration, we open a new tab in the terminal to view its title:
Consequently, the terminal shows author@linux:~ as the title of the new terminal tab:
- author represents the username of the currently logged-in user
- linux indicates the hostname of the machine
- ~ is the shorthand representation of the current working directory which is the HOME directory
However, we might prefer to change the title of the current terminal tab as per our requirements.
3. Using the PS1 Environment Variable
The PS1 environment variable defines the appearance of the command prompt in Linux-based systems. Moreover, it enables us to customize the prompt, including elements like the current working directory, hostname, and more.
In addition, we can use PS1 to dynamically change the title of the current terminal tab.
For instance, let’s use the PS1 environment variable and set the new title format of our current terminal tab:
$ PS1='\[\e]0;Baeldung\a\]\u@\h\w\$ '
In the script above, we use many placeholders within PS1 by setting ‘\[\e]0;MyTerminal\a\]\u@\h\w\$ ‘ as the format of the command prompt in the terminal:
- \u represents the username of the current user, which we follow with a literal @ for visual separation
- \h represents the hostname or the machine name of the system
- \w represents the current working directory
- \$ refers to the prompt symbol for the regular user, which we follow with a space for visual separation
In addition, we introduce non-printing characters starting between \[ and \]. Specifically, we use the \e]0; special escape sequence to introduce the title of Baeldung and end that with \a.
Let’s check the result:
Thus, we now see Baeldung as the title of the current terminal tab.
Moreover, in the terminal, PS1 displays author as the username, linux as the hostname, and ~ as the current working directory along with the $ prompt symbol.
4. Using PROMPT_COMMAND With PS1
The PROMPT_COMMAND variable enables us to set a command to run before the primary prompt is displayed. So, we can use it in combination with the PS1 variable to perform other actions, such as updating the title of the current terminal tab.
For this, first, we define the terminal format using the PS1 variable:
$ PS1="\u@\h:\w\$ "
Here, PS1 displays the \u username, @ literal character, \h hostname, : symbol, and the \w current working directory, followed by the $ prompt symbol in the terminal.
Next, we set the value of the PROMPT_COMMAND variable to change the current tab title:
$ PROMPT_COMMAND='echo -ne "\033]0;Baeldung\007"'
In the script above, the PROMPT_COMMAND executes the echo command, which uses the \033]0;Baeldung\007 ANSI escape sequence to set the current tab title to Baeldung every time the prompt is displayed.
Further, let’s break down the \033]0;Baeldung\007 ANSI escape sequence:
- \033 octal value in ASCII represents the start of an escape sequence (this is the same as \e)
- ] represents the end of the control sequence and the start of the operating system command
- 0; indicates the operating system command that sets the terminal title
- Baeldung is the desired title of the terminal tab
- \007 octal value in ASCII represents the end of the operating system command
Let’s check the result:
Thus, we can see the updated title of the current terminal tab on our Linux terminal.
5. Editing the ~./bashrc File
We can also edit the ~./bashrc file to create a helper function that modifies the current tab title of our terminal.
For this, we open the ~./bashrc file using a text editor like nano:
$ nano ~/.bashrc
Then, we define a set-title() function in the ~/.bashrc file:
function set-title() {
if [[ -z "$ORIG" ]]; then
ORIG=$PS1
fi
TITLE="\[\e]2;$*\a\]"
PS1=${ORIG}${TITLE}
}
In the above set-title() function, the if statement with the -z option checks if the ORIG variable is empty. If so, it stores the current prompt $PS1 in ORIG.
Next, we set \[\e]2;$*\a\] escape sequence as the value of the TITLE variable. Moreover, this variable sets the terminal tab title based on the provided argument in the shell.
At last, the set-title() function updates $PS1 to include the custom TITLE as we did previously.
After adding the code, we press CTRL+O and Return to save the ~./bashrc file and CTRL+X to exit the nano editor:
Next, we run the source command to apply the changes made to the ~./bashrc file:
$ source ~/.bashrc
Finally, we call the set-title function and pass Baeldung as an argument to it:
$ set-title Baeldung
Now, let’s check the result:
Consequently, the changes reflect the title of our current terminal tab.
6. Conclusion
In this article, we explored the methods for changing the title of the current terminal in Linux CLI.
Particularly, we used the PS1 or the PROMPT_COMMAND variables to change the current tab title directly from the CLI. On the other hand, we can edit the ~./bashrc file and define a function for the purpose, so it’s more readily available when needed.
Ultimately, we can select any of these methods based on our preferences for changing the title of the current terminal tab on our system.