The read command takes input from the user or a file and stores it into one or more variables.
Click to reveal answer
beginner
How do you read multiple values into separate variables in bash?
Use read var1 var2 var3. Each word from the input is assigned to a variable in order.
Click to reveal answer
intermediate
What happens if the input has more words than variables when using read?
The last variable gets all remaining words as a single string.
Click to reveal answer
beginner
How can you prompt the user with a message before reading input into variables?
Use read -p "Your message: " var1 var2 to show a prompt before input.
Click to reveal answer
beginner
Why is reading into multiple variables useful in scripting?
It lets you capture several pieces of related input at once, like a first and last name, making scripts interactive and flexible.
Click to reveal answer
What does the command read a b do when you enter: "hello world"?
AStores 'hello' in a and 'world' in b
BStores 'hello world' in a and leaves b empty
CStores 'hello' in b and 'world' in a
DThrows an error because of two variables
✗ Incorrect
The first word goes to the first variable, the second word to the second variable.
If you run read x y and input "one two three", what will be stored in y?
A"two"
B"three"
C"two three"
DEmpty string
✗ Incorrect
The last variable gets all remaining words after the first variable.
How do you show a prompt message before reading input in bash?
Aecho "Enter: " read var
Binput -p "Enter: " var
Cprompt "Enter: " var
Dread -p "Enter: " var
✗ Incorrect
The -p option with read shows a prompt.
What happens if you use read with only one variable and input multiple words?
AOnly the first word is stored
BAll words are stored as one string
CAn error occurs
DThe input is ignored
✗ Incorrect
All input words go into the single variable as one string.
Which command reads three words into variables a, b, and c?
Aread a b c
Bread abc
Cread -3 a b c
Dread -abc
✗ Incorrect
Listing variables separated by spaces after read assigns input words accordingly.
Explain how the read command assigns input words to multiple variables in bash.
Think about how words from a sentence are given to each variable.
You got /3 concepts.
Describe a simple script that asks for a user's first and last name and stores them in two variables.
Use read -p to show a message and read two variables.
You got /4 concepts.
Practice
(1/5)
1. What does the read command do when used with multiple variables like read a b c in a bash script?
easy
A. It assigns the first word to c, second to b, and third to a.
B. It assigns all input words to a only.
C. It throws an error because read cannot use multiple variables.
D. It assigns the first word to a, second to b, and the rest to c.
Solution
Step 1: Understand how read splits input
The read command splits input by spaces and assigns each word to the variables in order.
Step 2: Check behavior with multiple variables
If there are more words than variables, the last variable gets all leftover words combined.
Final Answer:
It assigns the first word to a, second to b, and the rest to c. -> Option D
Quick Check:
Multiple variables split input words in order [OK]
Hint: Last variable gets all leftover input words [OK]
Common Mistakes:
Thinking all words go to the first variable
Assuming variables get words in reverse order
Believing read cannot handle multiple variables
2. Which of the following is the correct syntax to read three variables name, age, and city from user input in bash?
easy
A. read name age city
B. read name, age, city
C. read(name age city)
D. read -v name age city
Solution
Step 1: Recall correct read syntax
The read command uses space-separated variable names without commas or parentheses.
Step 2: Identify correct option
read name age city uses read name age city, which is the proper syntax to read into three variables.
Final Answer:
read name age city -> Option A
Quick Check:
Variables separated by spaces in read [OK]
Hint: Use spaces, no commas or parentheses in read [OK]
Common Mistakes:
Adding commas between variable names
Using parentheses like a function call
Using unsupported flags like -v
3. What will be the output of this script if the user inputs: apple banana cherry date?
read a b c
echo "$a"
echo "$b"
echo "$c"
medium
A. apple
banana cherry
date
B. apple
banana
cherry
C. apple
banana
cherry date
D. apple banana cherry date
Solution
Step 1: Understand variable assignment with extra words
The first two variables a and b get the first two words: "apple" and "banana".
Step 2: Assign leftover words to last variable
The last variable c gets all remaining words combined: "cherry date".
Final Answer:
apple
banana
cherry date -> Option C
Quick Check:
Last variable holds leftover words [OK]
Hint: Last variable collects all leftover input words [OK]
Common Mistakes:
Expecting only one word in last variable
Assuming variables get input in reverse order
Confusing echo output formatting
4. Consider this script snippet:
read x y z
echo "$x"
echo "$y"
echo "$z"
If the user inputs only two words, what is the problem and how to fix it?
medium
A. Script will crash; fix by adding quotes around variables.
B. Variable z will be empty; fix by providing default value or checking input count.
C. Variables x and y swap values; fix by reversing variable order.
D. Input will be assigned to z only; fix by using arrays.
Solution
Step 1: Analyze behavior with fewer input words
If input has fewer words than variables, the extra variables get empty values. Here, z will be empty.
Step 2: Suggest fix for empty variable
To avoid issues, check input count or assign default values before using variables.
Final Answer:
Variable z will be empty; fix by providing default value or checking input count. -> Option B
Quick Check:
Missing input words cause empty variables [OK]
Hint: Check input length before using variables [OK]
Common Mistakes:
Expecting script to crash on missing input
Assuming variables auto-fill with defaults
Ignoring empty variable values
5. You want to read a user's full name (which may have multiple words) into two variables: first and last. The first variable should get the first word, and the second variable should get the rest of the name. Which command correctly does this?
hard
A. read first last
B. read -a first last
C. read first; read last
D. read first rest; last=$rest
Solution
Step 1: Understand how read assigns leftover input
Using two variables with read first last assigns the first word to first and all remaining words to last.
Step 2: Evaluate other options
read -a first last uses invalid flag -a incorrectly; read first; read last reads twice, not splitting input; read first rest; last=$rest is invalid syntax.
Final Answer:
read first last -> Option A
Quick Check:
Two variables split input: first word and rest [OK]
Hint: Use two variables; last gets all leftover words [OK]
Common Mistakes:
Using invalid flags with read
Trying to read twice instead of splitting input
Not knowing last variable collects leftover words
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.