Customizing Bash Prompt with PROMPT_COMMAND
How to customize your terminal prompt using the PROMPT_COMMAND environment variable in Bash
February 19, 2026
Customizing Bash Prompt with PROMPT_COMMAND
In Bash, the
PROMPT_COMMAND environment variable allows you to execute specific commands just before a new prompt is displayed.This allows you to display a real-time Timestamp or custom information in your terminal prompt.
Basic Usage
For example, entering the command below will display the current date and time before the prompt.
Set PROMPT_COMMANDsh
Permanent Configuration
To ensure the settings persist even after restarting the terminal, you must add them to your
~/.bashrc file.~/.bashrcsh
Colorized Format
You can use ANSI Escape Codes to add color and keep the prompt from looking cluttered.
Colorful Promptsh
How to Disable
To clear the
PROMPT_COMMAND you set and return to the default state, use the unset command.Unset PROMPT_COMMANDsh
Why use PROMPT_COMMAND
While you can also decorate the prompt by modifying the
PS1 variable, PROMPT_COMMAND is more powerful in the following situations:- Executing complex logic: Beyond simply showing text, you can run specific scripts or record logs.
- Dynamic information update: Suitable for calculating and showing the number of files in the current working directory or Git branch status in real-time.