Interactive versus non-interactive scripts
The script we have created so far uses user input, but it can't really be called interactive. As soon as the script is fired off, with or without arguments to the parameters, the script runs and completes.
But what if we do not want to use a long list of arguments, instead prompting the user for the information that is needed?
Enter the read
command. The basic usage of read
looks at input from the command line, and stores it in the REPLY
variable. Try it out yourself:
reader@ubuntu:~$ read This is a random sentence! reader@ubuntu:~$ echo $REPLY This is a random sentence! reader@ubuntu:~$
After you start the read
command, your terminal will go down a line and allow you to type anything you want. As soon as you hit Enter (or, actually, until Bash encounters the newline key), the input will be saved into the REPLY
variable. You can then echo this variable to verify it has actually stored your text.
read
has a few interesting flags which make it more usable...