Sample Header Ad - 728x90

Cannot modify bash history inside command_not_found_handle function

4 votes
1 answer
258 views
I'm trying to make my own command_not_found_handle function, which on given a wrong command, asks the user with the most identical command if that was what he meant. If the user accepts that suggestion then it should delete the last command (the wrong command) from the history and add the selected one. But it isn't working. This is my code
command_not_found_handle(){
  ....

  # replace the previous wrong command with the correct one in bash history
  history -d -1
  history -s "$command $@"
}
When I enter a wrong command and select one from the suggestion, it doesn't modify the history. But if I call the function myself e.g.
command_not_found_handle wrong_command
It updates the history, removing the wrong_command and appending the new one. I verified that the function has read access to the history in both cases by calling the history command inside the function, which returns the history list. But in the first case it cannot write to the history.
Asked by Sunlight (43 rep)
Oct 19, 2022, 03:00 AM
Last activity: Nov 28, 2022, 10:13 PM