One alternative way to do this would be to just redirect standard input to your file, where you have all the user input in the order it's expected by the program. For example, with the program (called
echo "Enter your name:"
echo "...and now your age:"
# example of how to use the values now stored in variables $name and $age
echo "Hello $name. You're $age years old, right?"
and the input file (called
you could run this from the terminal in one of the following two ways:
$ cat input.in | ./script.sh
$ ./script.sh < input.in
and it would be equivalent to just running the script and entering the data manually - it would print the line "Hello Tomas. You're 26 years old, right?".
As Radu Rădeanu has already suggested, you could use
cat inside your script to read the contents of a file into a avariable - in that case, you need each file to contain only one line, with only the value you want for that specific variable. In the above example, you'd split the input file into one with the name (say,
name.in) and one with the age (say,
age.in), and change the
read name and
read age lines to
name=$(cat name.in) and
age=$(cat age.in) respectively.