Bulk Respond To Confirmation Prompts in Linux

Published by Torry Crass on

This is quite the little zinger from time to time so I thought I would jot down a quick post on it, mainly for my memory, and for anyone else who might be looking for a similar solution.

For example, when you run the following, you could be required to respond to a number of different confirmation prompts asking if you really want to execute the command.

cp -rf source destination

If you are working on a really large file copy or move, this might be ridiculously tedious and, in some cases, impractical.  There are a few ways to tackle this such as using the expect function.  However, for most purposes, this is often too complex of a process for a simple command line response but could be useful if you need to programmatically handle different responses or have a security need.  I will not cover these examples here since it is out of scope for this post.


The first, and probably easiest blanket way to handle responses is to use pipe to answer the prompts.  This will send the response to the command or script whenever it pauses for user input.  Such as this example:

yes | cp -rf source destination

Individual Response

The second, if you're only responding to a few prompts, such as in creating a custom install script, is to use echo and pipe in conjunction with your script to answer the prompts individually.  Such as this example:

echo "ANSWER1\nANSWER2\nANSWER3\n" | yourscript.sh

In this case, it is important to note the \n after each answer.  In some cases you may want to use echo -e or, depending on your script, may want to invoke it using sh ./yourscript.sh, but this should at least get you started.


Leave a Reply