| Lesson 6 |
A command to read keyboard input |
| Objective |
Use the read command to collect user input in Unix |
Using read to Collect Keyboard Input
Many scripts run unattended, but interactive scripts ask questions and respond to what the user types.
In Unix shell scripting, the
read command is the standard way to collect keyboard input and store it in a variable so your script can make decisions or take actions based on that input.
In this lesson you will:
- prompt the user for input
- store the response in a variable
- print the value back to the screen as verification
Two variable rules you will use immediately:
- You can name variables anything you want. Use descriptive names (for example
MYNAME, RESPONSE, or DOWNLOADFILE).
- To use the value stored in a variable, prefix the name with
$ (for example $MYNAME).
Basic read Syntax
The simplest form of
read takes one parameter: the variable name that should receive the user’s input.
A common pattern is:
echo "Enter the name of the file to download:"
read DOWNLOADFILE
When the script runs, the user sees the prompt and the cursor waits for input. After the user presses Enter, the text they typed is stored in
DOWNLOADFILE.
Using the Variable Value
Once you have captured input, you can use it anywhere in your script. A simple verification step is to print the value back to the user.
For predictable formatting,
printf is a strong choice:
printf "The name of the file you entered was: %s\n" "$DOWNLOADFILE"
Example output:
The name of the file you entered was: cruise_update.html
From here, scripts typically validate the input (for example, check if a file exists) before using it.
Modern Best Practices for read
Different shells support different options. The following practices are widely useful:
- Use
read -r when possible: this prevents backslashes from being treated as escape characters.
- Always quote variables: use
"$VAR" to avoid bugs with spaces or special characters.
- Validate input: check for empty values and confirm files exist before acting on user input.
Example: prompt, read safely, and reject empty input:
echo "Enter a filename:"
read -r DOWNLOADFILE
if [ -z "$DOWNLOADFILE" ]; then
echo "ERROR: You must enter a filename." 1>&2
exit 1
fi
If you are specifically using bash, you may also see:
read -p "Prompt: " VAR to print the prompt without a separate echo
read -s VAR to read input silently (useful for passwords)
read -t 10 VAR to time out after 10 seconds
Using the read Command
The following sequence demonstrates a complete interactive input flow:
- Start a Bourne shell (optional):
sh
- Print the prompt:
echo "Please enter your name:"
- Read a line of text into a variable:
read -r MYNAME
- Type your name and press Enter
- Print a confirmation message using the variable:
printf "Thank you, %s\n" "$MYNAME"
Later lessons expand this idea by using
read inside loops to process files line-by-line and by combining input with conditional tests.
Read Keyboard Input Command - Exercise
Click on the Exercise link below to practice what you have learned in the UNIX Lab.
Read keyboard
Input command - Exercise
The next lesson shows you how to prepare a script file for execution.
