chinmay chhajed
Posted on February 20, 2021
Bash prompt can be set by setting the environment variable PS1
. We need to update this variable to get time in our prompt.
There are 4 ways to get the time in prompt. If you see manual page for bash
, you can see time can be set using:
\t the current time in 24-hour HH:MM:SS format
\T the current time in 12-hour HH:MM:SS format
\@ the current time in 12-hour am/pm format
\A the current time in 24-hour HH:MM format
Note that the time is not the current time but the time when prompt was last updated. Prompt will be updated when previous command completed.
Sample usage:
PS1="\t $ " # 11:13:54 $
PS1="\T $ " # 11:14:01 $
PS1="\@ $ " # 11:14 AM $
PS1="\A $ " # 11:14 $
Add whichever style you like to your $HOME/.bashrc
and every time you open a new terminal, you will see time in command prompt.
If you wish to show current directory with time in prompt, a nice prompt setting could be like
PS1="\w \A $ "
PROMPT_DIRTRIM=2
# Resulting in prompt to look like
# ~/.../bluetooth/nimble 11:16 $
Here PROMPT_DIRTRIM
would set the number of parent directories you wish to see in the prompt.
Checkout designing a minimal bash prompt.
Posted on February 20, 2021
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.