Alice
Alice

Reputation: 1400

Display the current time on the prompt by configuring PS1

I am trying to configure PS1 to display the current time, here is my current variable

me@host:~/Downloads$ env | grep -i 'ps1'
PS1=\[\e]0;\u@\h: \w\a\]${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ 

The result planed is

me@host; 09:00 AM: ~/Downloads$

\T is expanded into the current time in 12-hour format,

Configure it temporally

me@host:~/Downloads$ export PS1="\[\e]0;\u@\h;\T: \w\a\]${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$"

Unfortunately, the prompt does not change.

What' s the problem with my usage?

Upvotes: 0

Views: 230

Answers (1)

jhnc
jhnc

Reputation: 16662

  1. If you double-quote, then variable expansion will strip out the debian_chroot sections
  2. Two settings are embedded in the string but you only set one:
    • the first (which you changed) sets the title of the terminal window
    • the second (which you didn't change) sets the command prompt

Try something like:

PS1='\[\e]0;\u@\h;\T: \w\a\]${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\];\T:\[\033[01;34m\]\w\[\033[00m\]\$ '

Upvotes: 1

Related Questions