0
0
Bash Scriptingscripting~20 mins

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

Choose your learning style9 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.