Re-evaluate PS1 in bash

2015-01-08 10:49

This is a little gem I found out when I changed my PS1 expression to include a counter for my todo list items in my prompt.

I'm usint t to organize my tasks and took the PS1 suggested on the site:

export PS1="[$(t | wc -l | sed -e's/ *//')] $PS1"

I soon found out that it only gets evaluated once when bash starts. The trick, it turns out, is to escape $ so that it gets interpreted literally when bash starts but then gets used as expression when PS1 is re-evaluated:

export PS1="[\$(t | wc -l | sed -e's/ *//')] $PS1"

So this, the tiniest possible change, got me a nice, live, task count showing in my prompt. A nice reminder that I still have work to do.