Introduction
Reading into multiple variables lets you get several pieces of information from one input line easily.
read var1 var2 var3 ...
read firstName lastName
read a b c
read x y # Input: hello world from bash # x=hello, y='world from bash'
#!/bin/bash echo "Enter your first and last name:" read firstName lastName echo "Hello, $firstName $lastName!"
read command do when used with multiple variables like read a b c in a bash script?read splits inputread command splits input by spaces and assigns each word to the variables in order.a, second to b, and the rest to c. -> Option Dread cannot handle multiple variablesname, age, and city from user input in bash?read syntaxread command uses space-separated variable names without commas or parentheses.read name age city, which is the proper syntax to read into three variables.read [OK]read [OK]apple banana cherry date?
read a b c echo "$a" echo "$b" echo "$c"
a and b get the first two words: "apple" and "banana".c gets all remaining words combined: "cherry date".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?
z will be empty.z will be empty; fix by providing default value or checking input count. -> Option Bfirst 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?read assigns leftover inputread first last assigns the first word to first and all remaining words to last.-a incorrectly; read first; read last reads twice, not splitting input; read first rest; last=$rest is invalid syntax.readPass the test and this pattern moves unknown