MacOS – Shell variable PROMPT_COMMAND not working as intended

bashcommand linemacosterminal

I want my bash prompt to change depending on external conditions.

Here is excerpt from the bash manual:

PROMPT_COMMAND: If set, the value is executed as a command prior to issuing each primary prompt.

PS1: The value of this parameter is expanded (see PROMPTING below) and used as the primary prompt string.

In my .bash_profile I have following:

export PROMPT_COMMAND="echo -n ┏━━━[$(date +%H:%M)]"
export PS1="━━[\t]━━━┓\n\$ "

Here is what I see in terminal:

┏━━━[03:46]━━[03:46:52]━━━┓
$ cd ..
┏━━━[03:46]━━[03:51:37]━━━┓
$

As you can see, PROMPT_COMMAND doesn't get executed more than once, and remains static forever.

How can I have it executed "prior to issuing each prompt", as stated in the manual?

I'm running Mac OS X 10.9.3
echo $BASH_VERSION
3.2.51(1)-release

Best Answer

The PROMPT_COMMAND gets run every time, but the $(...) snippet is only evaluated when your .bash_profile is loaded. This is because the double quotes mean to still expand variables and commands.

If you use single quotes, the command is not substituted during the execution of .bash_profile, it is evaluated when the PROMPT_COMMAND is run.