Reputation: 29
How to read any number of inputs from user at once and store it into an array. For example this will read only a single input at a time
read -p "Please enter username: " username
echo "Dear $username,"
I want something like:
read -p "Please enter location names separated by space: "
How would i store location names into an array and then loop through it.
Upvotes: 1
Views: 19625
Reputation: 4154
try this:
#!/bin/bash
read -r -p "Please enter location names separated by space: " -a arr
for location in "${arr[@]}"; do
echo "$location"
done
Upvotes: 3
Reputation: 2699
read -p "Please enter location names separated by space: " location_names
for location in $location_names
do
echo $location
done
Testing.
Please enter location names separated by space: paris london new-york
paris
london
new-york
Upvotes: 1