i wanted to implement a cool feature for my PS1 prompt. I added \# string to show the command number of that command. My goal is that if a certain command is ran, the command number from \# to be set back to 1. I have some ideas but i don't know how to write it.
Is it possible to somehow export \# value as a variable, and when a command is run, the variable to be restored to the initial state. Actually this post says that its impossible.
If this doesn't work, can i create a counter that replaces \#, and then export it as a variable. That variable to be reset by a certain command.
https://www.baeldung.com/linux/bash-script-counter
Sorry i don't have enough knowladge in bash, hope i don't ask for the impossible. Thanks
1 Answer
I ran a quick test with a trap. This worked for me -
$: trap 'echo "$BASH_COMMAND"; [[ "$BASH_COMMAND" =~ ^:\ *reset ]] && trap -- debug' debug
set_titlebar
set_prompt
$: date
date
Fri Jun 25 08:29:26 CDT 2021
set_titlebar
set_prompt
$: : reset
: reset
$: date
Fri Jun 25 08:29:38 CDT 2021
$:
The trap checks $BASH_COMMAND for your key (I used : reset) and resets the trap when seen. Edit in whatever key and/or code you want.
if [ "\$(history| tail -n1)" -ne "<your reset command>" ];instead ofif [ $? -eq 0 ];will not do the trick ? Beware that you can't use!-1in a prompt context.if [ "\$(history| tail -n1| sed 's/^ *[0-9][0-9]* //')" -ne "<your reset command>" ];, to get rid of the history line number.