I want to modify the command_not_found_handler function in zsh so that after execution, it prints the package installation command related to the command on the screen and stores its command in a global variable.
# this function provides command to install that packge if its not installed
command_not_found_handler() {
cmd="$1"
prt="zsh: command not found: $cmd"
package=$(pacman -Ss "^$cmd$" | grep "/$cmd " | awk -F '/' '{print $2}' | cut -d' ' -f1)
SUGGESTION_CMD="NOT FOUND"
if [[ -n "$package" ]]; then
SUGGESTION_CMD="pacman -S $package"
prt="$prt ($SUGGESTION_CMD)a"
fi
echo $prt
return 127
}
export SUGGESTION_CMD
But after each execution of this function (if a command is entered and its package is not installed), the value of the variable is not updated globally.
$ gimp
zsh: command not found: gimp (pacman -S gimp)
$ echo $SUGGESTION_CMD
$ command_not_found_handler
zsh: command not found:
$ echo $SUGGESTION_CMD
NOT FOUND
I am a newbie myself and I also used chatgpt but I didn’t get a response.