4

I am trying to have a confirmation message every time I type exit command in the command-prompt. To do this, I have tried to use trap in .bashrc file but it seem like trap is not a solution as it run the original command anyway. Is there a way I can have this?

Here is my bashrc script code which could not get the job done:

function _exit()        # Function to run upon exit of shell.
{
    read -p "${RED}Are you sure? " REPLY
    if [[ $REPLY =~ ^[Yy]$ ]]; then
        echo -e "${RED}Bye${NC}"
        exit 0
    else
        #I do not know what to do here to not exit
        return
    fi  
}
trap _exit EXIT

2 Answers 2

6

If the shell is zsh or bash (though not in sh mode), make exit a function. Functions have precedence over shell builtins (even special ones like exit) in zsh or bash (though not in POSIX shells). So just rename your function to exit and use command exit within the function instead. Otherwise you had endless recursion, of course.

0
1

Overload function exit() won't fit the ^D scenario. So, let's talk about trap.

Your else clause should be some things that can either hold bash or ignore the exit signal. I don't know how to do that either.

But for now, alternatively, I think you can re-open bash.

If you replace your else clause with this:

echo
history -a
bash

And then if you cancel the exit on the prompt, a new bash will be re-opened, and it'll looks much like nothing happend, just like in the old one.

This approach does have a flaw, you will lose the variable assignments you typed in before, may be we can work on that. But if you're cool with this, this is it.

You can modifiy this code to fit your needs anyway.

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.