Ubuntu – always prompt the user before executing a command in the shell

bashcommand line

Is there a utility that always prompts the user for confirmation before executing a command similar to the way sudo asks for password?

Best Answer

Do you want it to work without typing an extra command, e.g.

$ rm file

Or only when the user types something like

$ confirm rm file

Or only when the user tries to run certain commands, e.g.

$ rm file

but not for

$ echo "Hello"

If option 1, that can be done using the preexec hook in zsh, or the DEBUG trap in bash.

If option 2, put something like this in /etc/bash.bashrc or other shell startup file.

confirm() {
    echo -n "Do you want to run $*? [N/y] "
    read -N 1 REPLY
    if test "$REPLY" = "y" -o "$REPLY" = "Y"; then
        echo "Cancelled by user"

If option 3, you could modify the confirm script above, or, some commands have an option to ask before doing something, e.g. rm -i. You could put alias rm='rm -i' in /etc/bash.bashrc.