Why is my function not re-evaluated in PS1?
According to Bash prompt Howto:
[21:58:33][giles@nikola:~]$ PS1="[\$(date +%H%M)][\u@\h:\w]\$ " [2159][giles@nikola:~]$ ls bin mail [2200][giles@nikola:~]$
It's important to notice the backslash before the dollar sign of the command substitution. Without it, the external command is executed exactly once: when the PS1 string is read into the environment.
When you used $(..)
in double-quotes, the shell evaluated the command substitution before assigning to PS1
. Thus, PS1
contained only the output, not the command substitution itself. Instead, either use single-quotes, or escape the $
, so that the string is passed as-is to PS1
, and then evaluated when the prompt is set:
$ PS1='$(pwd) $ '
/tmp $ cd /var
/var $ echo "$PS1"
$(pwd) $
Compare:
/var $ PS1="$(pwd) $ "
/var $ echo "$PS1"
a /var $ a
/var $