
Learn through the super-clean Baeldung Pro experience:
>> Membership and Baeldung Pro.
No ads, dark-mode and 6 months free of IntelliJ Idea Ultimate to start with.
Last updated: March 18, 2024
In this tutorial, we’ll explore various ways to add a clock to shell prompts. We’ll begin with Bash and implement a basic clock that updates interactively. Next, we’ll write a simple Bash script that renders a live clock inside the terminal.
In addition to Bash, we’ll also learn how to create a live clock for the Zsh prompt without relying on any external plugins. Finally, we’ll provide a couple of generic solutions that should apply to most shells.
By default, the Bash shell prompt displays the username, hostname, and current directory:
user@baeldung:~$
Usually, the prompt is set with the PS1 environment variable:
$ echo $PS1
\[\e]0;\u@\h: \w\a\]${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$
Let’s update this environment variable to show the current time:
$ PS1='\[\u@\h:\T:\w\$'
Let’s break this down:
Once we set the prompt, it now displays a clock:
baeldung@baeldung:09:56:50:~$
However, we should know that the clock updates only when we press the Return key:
baeldung@baeldung:10:01:01:~$^C
baeldung@baeldung:10:01:07:~$^C
baeldung@baeldung:10:01:08:~$^C
baeldung@baeldung:10:01:09:~$^C
Alternatively, it also updates when we send SIGINT (interrupt signal) through Ctrl+C.
Nevertheless, if we prefer a real live clock, we can write our own using tput and date:
while sleep 1;
do tput sc;
tput cup 0 $(($(tput cols)-11));
echo -e "\e[32m`date +%r`\e[39m";
tput rc;
done &
Let’s dig into this:
Here’s the clock:
Now, if we want this to persist across the shell sessions, we can simply put this in ~/.bashrc:
$ echo 'while sleep 1; do tput sc; tput cup 0 $(($(tput cols)-11)); echo -e "\e[32m`date +%r`\e[39m"; tput rc; done &' >> ~/.bashrc
By default, Zsh provides a lot of flexibility. In addition to the available options, there are many plugins written for different purposes. However, for our use case, we’ll implement the clock manually.
In Zsh, there is a built-in command zle (Zsh line editor). It lets us customize the behavior of the prompt via custom keymappings, functions, and widgets.
Additionally, we can use it with the reset-prompt argument, which forces the prompt to re-evaluate and re-render the edit buffer. In other words, zle reset-prompt updates and refreshes the prompt to render dynamic content.
The config file for the interactive shell is ~/.zshrc. So, let’s add the config for the live clock here:
...
setopt PROMPT_SUBST
PROMPT='%B%F{red}%n@%m%f%F{yellow}[%D{%L:%M:%S}]%f:%F{blue}${${(%):-%~}}%f$ %b'
TMOUT=1
TRAPALRM() {
zle reset-prompt
}
Let’s explore what happens here:
In summary, the code creates a prompt that contains the current time and updates it every second. When we put this code inside the config file and restart the shell session, it displays a clock in the prompt:
In this section, we expand on methods that should work on all shells.
Alternatively, if we just want to display a live clock in the terminal and nothing else, we can use this command:
$ watch -t -n1 "date +%T|figlet"
Let’s dissect this:
Here’s how it looks:
Fortunately, tmux doesn’t need any work. By default, it displays the current time in the status bar:
However, if we want to remove the date, then we simply add this line to ~/tmux.conf:
status-right " "#{=21:pane_title}" %H:%M"
In this article, we learned how we can customize a shell to display a live clock. We delved into the details of implementing it for both Bash and Zsh shells.
In addition, we also discussed how to display a live clock that works on most shells and terminals.