11

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

2
  • 1
    Do you really want that for any command? Including cd and ls? Jan 19, 2011 at 8:16
  • I'd like to have it in my own aliases in .bashrc.
    – m33lky
    Jan 20, 2011 at 5:03

3 Answers 3

10

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.

1
  • I would like the confirmation only for one single command that always gives me headache. How would the script look like?
    – Danijel
    Apr 17, 2019 at 8:03
10

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.

3
  • Stops on Allow? 1) yes 2) no #? neither yes nor no work causing infinite loop (Debian 8)
    – vladkras
    Jan 13, 2016 at 15:13
  • @vladkras read help select: you're supposed to input 1 or 2 rather than yes or no. The automatic retry for invalid inputs is intended behavior (so that the script writer doesn't have to reinvent a while herself, retaining conciseness and clarity), and EOF input terminates the loop. Knowing that, you can check yourself that the script above works. I won't be modifying it, since it's a crude proof of concept anyway.
    – ulidtko
    Jan 14, 2016 at 13:58
  • that's true. I was confused by your y e s Enter example
    – vladkras
    Jan 14, 2016 at 16:54
0

First, open the terminal.

Then, type

cd ~
touch .sure
chmod 700 .sure

Next, open .sure and paste this inside.

#!/bin/bash --init-file
PS1='> '
alias y='
    $1
    exit
'
alias n='killall Terminal'
echo ''
echo 'Are you sure? Answer y or n.'
echo ''

After that, close the file.

~/.sure ; ENTER COMMAND HERE

This will give you a prompt of are you sure before continuing the command.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .