Collecting User Input in a Bash Shell Script

The read command is designed to read and then use in the script input from the user. The input that is provided by the user is stored as a variable. This is a builtin variable that will store one line of input from the user in one or more variables. The read command is valuable as it is a major way to input information into a shell script. Lines are read into the script with standard input and split via the $IFS variable. This stands for internal field separator. The first word is assigned the first variable, the second word the second variable, etc.
Read options
-a name assign each work in an array of the name
-d delim use a deliminator to terminate input instead of new line
-e input from keyboard uses Readline Library, The Readline Library is a set of library routines that controls the way keys function when typing text at the command line.
-n num reads number of characters and then returns
-p prompt displays prompt on errors
-r ignore the backslash
-s do not echo characters entered, silent to screen
-u n use the integer n as the file descriptor

The user is asked to input their username which becomes the variable $name. A sleep command is entered which just causes the script to stop for 3 seconds to act as if processing is occurring in a database and then the line that matches the user is printed.
End of Build Your Knowledge

Variables and Text
Since the variable that is used can only have one value, quotes are used to set this specific value. The shell does not care what kind of strings you place inside the quotes. So you could say;
REPORT=”This is the 33rd folder I’ve created today, what’s up?”

The Bash shell does not require that you declare your values, however it just provides an empty string if you do not declare them. So in the following script the STATUS variable just outputs an empty string.

It is important to start and end your variables so the shell can tell where the variable ends. In this example, the variable FOLDER is confused with “_for_mike” because the shell cannot tell where the variable ends. So it is important to add braces around the variable in this situation.

Official Launch

Now that we've officially launched BashShell.net, you will find new Bash Shell tutorials arriving daily. Please be patient as we increase the difficulty of the projects and present more advanced Bash Shell examples over time.