Wow, that got really mangled somewhere! Here's the main part of the test script again. Hopefully it'll come through OK this time.
histt() { local space='' space="${1:+ }" # set space to a space of there's a $1 local hist local comment hist=$(history 1) hist="${hist# *[0-9]* }" echo "....before...." history 3 comment="some comment" history -s "$space$hist # $comment" echo "....after...." history 3 PROMPT_COMMAND=$previous_prompt_command # put things back like you found them unset previous_prompt_command unset histt } previous_prompt_command=$PROMPT_COMMAND PROMPT_COMMAND="histt $1" # set up and run it