How to pass Enter key as input value in shell script using another shell script

Viewed 46

I have a setup.sh shell script which needs to be run on Ubuntu to setup something.

I have made another shell script (say sample.sh) which installs pre-requisites before running 'setup.sh' and then finally it triggers 'setup.sh'

setup.sh script for example contains the below sample prompts which require user inputs

  1. Press Enter to use default:

How to provide Enter key press as input instead of manually entering.

1 Answers

If the prompts are simple then you can pass the desired inputs on stdin. A heredoc-based example might look like:

#!/bin/bash
# blank line for "Press Enter"
# "y" for response to y/n question
# "string-1 value" for the response to that prompt
./setup.sh <<EOF

y
string-1 value
EOF

If the prompts are more complex, like if there's branching logic based on user inputs, then you may need a more powerful tool like expect.

Related