3

In bash I can do:

#!/bin/bash
validate() {
    if [[ "$BASH_COMMAND" == whoami ]]; then
        return 1
    else
        return 0
    fi
}
set -T
trap 'validate' DEBUG
shopt -s extdebug

so that if I run whoami nothing happens.

How can I achieve the same in zsh ? I've looked at preexec but I couldn't find much information in the documentation

Foo
  • 232

1 Answers1

5

You can also use the DEBUG trap to skip a command in zsh, but the mechanism to indicate skipping is different: you need to set the err_exit option.

function validate {
    if [[ "$ZSH_DEBUG_CMD" == whoami ]]; then
        setopt err_exit
    fi
}
trap 'validate' DEBUG