Bash Script – Implement Batch Option –yes

bashshell-script

I have several user input statements like:

read -r -p "Do u want to include this step (y) or not (n) (y/N)"? answer
if [[ "$answer" =~ ^[Yy]$ ]]; then 
    ...
fi

I am looking for a way to automatically answering yes to all these questions. Imagine a non-interactive session where the user invokes the script with --yes option. No further stdin input.

The only way I can think right now is adding another condition on each if statement.

Any thoughts?

Best Answer

If you use read only for these questions, and the variable is always called answer, replace read:

# parse options, set "$yes" to y if --yes is supplied
if [[ $yes = y ]]
then
    read () {
        answer=y
    }
fi
Related Question