I've been using a snippet in my .bashrc that updates my terminal emulator's title bar with the currently running command or the working directory when nothing is executing. The line I'm using is a DEBUG trap:
trap 'test "$BASH_COMMAND" == "$PROMPT_COMMAND" && printf "e]2;$PWDa" || printf "e]2;$BASH_COMMANDa"' DEBUG ;;
While this has been working great for me, I recently discovered the $PS0 prompt, which also runs before a command executes. I thought it might be a better way to achieve the same result. However, I'm facing an issue where $PS0 updates the terminal title to the command that was just executed, leaving it one step behind. Is there any way to get $BASH_COMMAND to update sooner or an alternative approach using something like `readline`? Should I stick with the DEBUG trap or explore this PS0 method further?
1 Answer
Another option would be to set the `PROMPT_COMMAND` to a custom function that handles this. You can do something like:
```
set_bash_prompt() {
local exit_code="$?"
PS1="[e]0;u@ha][$(date +%F) $(date +%H:%M) | exit: $exit_code | w]n$ "
}
PROMPT_COMMAND=set_bash_prompt
```
This runs right before the prompt is displayed but just keep in mind it only updates after the command completes, not while it's running, so it might not serve your purpose exactly.
Your suggestion did inspire me to rethink my setup! Simplifying it to just update the title with $PWD while using the DEBUG trap has also helped make my .bashrc cleaner.

I see what you're saying, but that still wouldn't let me show the active command as it's running. The way the timing works with PROMPT_COMMAND makes it tricky.