DEV Community

Jacopo Valanzano
Jacopo Valanzano

Posted on

Linux Command Confirmation

Ever wanted a command that asks for confirmation before executing another command?

No? Well, me neither!

Here's a command that will ask for your confirmation before running the next command:

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
}
Enter fullscreen mode Exit fullscreen mode

It's also very easy to use:

root@root:~# confirm echo "Hello world!"
Do you want to run echo Hello world!? [N/y] y
Hello world!
Enter fullscreen mode Exit fullscreen mode

Top comments (0)

Billboard image

Create up to 10 Postgres Databases on Neon's free plan.

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Try Neon for Free →