Bird
Raised Fist0
Bash Scriptingscripting~20 mins

Reading into multiple variables in Bash Scripting - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Master of Reading into Multiple Variables
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
1:30remaining
What is the output of this Bash script reading into multiple variables?
Consider the following Bash script that reads a line of input into three variables. What will be the output if the input is apple banana cherry?
Bash Scripting
read a b c <<< "apple banana cherry"
echo "$a"
echo "$b"
echo "$c"
A
apple banana cherry

B
apple banana
cherry
C
apple
banana cherry
D
apple
banana
cherry
Attempts:
2 left
💡 Hint
Remember that the read command splits input by spaces and assigns each word to a variable in order.
💻 Command Output
intermediate
1:30remaining
What happens when input has more words than variables in Bash read?
Given this script, what will be the output if the input is red green blue yellow?
Bash Scripting
read x y z <<< "red green blue yellow"
echo "$x"
echo "$y"
echo "$z"
A
red green
blue
yellow
B
red
green
blue
C
red
green
blue yellow
D
red green blue
yellow
Attempts:
2 left
💡 Hint
The last variable gets all remaining words after the previous variables are assigned.
🔧 Debug
advanced
2:00remaining
Why does this Bash script fail to assign variables correctly?
This script is intended to read three words into variables a, b, and c, but it does not work as expected. What is the error?
Bash Scripting
read a, b, c <<< "one two three"
echo "$a $b $c"
ASyntaxError: commas are not allowed in variable list for read
Ba=one, b=two, c=three
Cone two three
Done two three with commas
Attempts:
2 left
💡 Hint
Check the syntax of the read command variable list.
💻 Command Output
advanced
1:30remaining
What is the output when reading input with IFS set to comma?
Given this script, what will be the output if the input is dog,cat,bird?
Bash Scripting
IFS=',' read animal1 animal2 animal3 <<< "dog,cat,bird"
echo "$animal1"
echo "$animal2"
echo "$animal3"
A
dog
cat,bird
B
dog
cat
bird
Cdog cat bird
D
dog,cat,bird

Attempts:
2 left
💡 Hint
IFS defines the delimiter for splitting input in read.
🚀 Application
expert
2:00remaining
How many variables are assigned when reading input with fewer variables than words?
Consider this script reading input with 5 words but only 3 variables. How many variables will be assigned and what is the value of the last variable?
Bash Scripting
read v1 v2 v3 <<< "one two three four five"
echo "$v1"
echo "$v2"
echo "$v3"
A3 variables assigned; v3='three four five'
B5 variables assigned; v3='five'
C3 variables assigned; v3='three'
D2 variables assigned; v3 is empty
Attempts:
2 left
💡 Hint
The last variable gets all remaining words after previous variables are assigned.

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

  1. Step 1: Understand how read splits input

    The read command splits input by spaces and assigns each word to the variables in order.
  2. Step 2: Check behavior with multiple variables

    If there are more words than variables, the last variable gets all leftover words combined.
  3. Final Answer:

    It assigns the first word to a, second to b, and the rest to c. -> Option D
  4. 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

  1. Step 1: Recall correct read syntax

    The read command uses space-separated variable names without commas or parentheses.
  2. Step 2: Identify correct option

    read name age city uses read name age city, which is the proper syntax to read into three variables.
  3. Final Answer:

    read name age city -> Option A
  4. 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

  1. Step 1: Understand variable assignment with extra words

    The first two variables a and b get the first two words: "apple" and "banana".
  2. Step 2: Assign leftover words to last variable

    The last variable c gets all remaining words combined: "cherry date".
  3. Final Answer:

    apple banana cherry date -> Option C
  4. 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

  1. 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.
  2. Step 2: Suggest fix for empty variable

    To avoid issues, check input count or assign default values before using variables.
  3. Final Answer:

    Variable z will be empty; fix by providing default value or checking input count. -> Option B
  4. 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

  1. 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.
  2. 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.
  3. Final Answer:

    read first last -> Option A
  4. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard