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
intermediate1: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"
Attempts:
2 left
💡 Hint
Remember that the read command splits input by spaces and assigns each word to a variable in order.
✗ Incorrect
The read command splits the input string by spaces and assigns each word to the variables a, b, and c respectively. So a='apple', b='banana', c='cherry'.
💻 Command Output
intermediate1: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"
Attempts:
2 left
💡 Hint
The last variable gets all remaining words after the previous variables are assigned.
✗ Incorrect
When there are more words than variables, the last variable gets all remaining words joined by spaces.
🔧 Debug
advanced2: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"
Attempts:
2 left
💡 Hint
Check the syntax of the read command variable list.
✗ Incorrect
The read command does not accept commas between variable names. Commas cause a syntax error.
💻 Command Output
advanced1: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"
Attempts:
2 left
💡 Hint
IFS defines the delimiter for splitting input in read.
✗ Incorrect
Setting IFS to comma makes read split input at commas, assigning each word to variables.
🚀 Application
expert2: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"
Attempts:
2 left
💡 Hint
The last variable gets all remaining words after previous variables are assigned.
✗ Incorrect
When fewer variables than words, the last variable gets all remaining words as a single string.