Hack 33. PROMPT_COMMAND

by Ramesh

Bash shell executes the content of the PROMPT_COMMAND just before displaying the PS1 variable.

  1. ramesh@dev-db ~> export PROMPT_COMMAND="date +%H:%M:%S"
  2.  
  3. 22:08:42
  4. ramesh@dev-db ~>
  5.  
  6. [Note: This displays the PROMPT_COMMAND and PS1 output on different lines]

If you want to display the value of PROMPT_COMMAND in the same line as the PS1, use the echo -n as shown below.

  1. ramesh@dev-db ~> export PROMPT_COMMAND="echo -n [$(date +%H:%M:%S)]"
  2.  
  3. [22:08:51]ramesh@dev-db ~>
  4.  
  5. [Note: This displays the PROMPT_COMMAND and PS1 output on the same line]