always prompt the user before executing a command in the shell

I have played with bash a bit and figured out a hack by which this could be made possible.

#!/bin/bash

debug_trap () {
    echo "executing $BASH_COMMAND"
    echo "Allow?"
    select choice in yes no
    do
        if [ "$choice" = "yes" ]
        then break
        elif [ "$choice" = "no" ]
        then return 1
        fi
    done
}

shopt -s extdebug
trap debug_trap DEBUG

You can save this script under, say, confirm-any-command.sh and source it in your bashrc. It will ask for confirmation of each command you will try to execute.

Please note that this is nothing more than a proof-of-concept hack; I doubt it can really be any useful in this form. If you will have to confirm each and every command you type, you will very soon acquire a habit to automatically hit "yes" after each command. Your mental mapping for "end of command" will change from just Enter to Enter,yes,Enter - you will be typing it as a whole, don't even trying to spend some time on verifying that you really want this command to execute. This is counterproductive and won't help you.


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
    echo
    if test "$REPLY" = "y" -o "$REPLY" = "Y"; then
        "$@"
    else
        echo "Cancelled by user"
    fi
}

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.