Reputation: 1109
Using read -p "ENTER 1 - 5 WORDS" v1 v2 v3 v4 v5
allows me to assign value to each of the variables v
by typing them on the command line. Is this the most practical approach? Suppose I wanted to assign a larger number of variables. Would I list each of them in this same way? I have tried read -p "ENTER 1-20 WORDS" {v1..v20}
which didn't work.
Upvotes: 1
Views: 768
Reputation: 52142
You might want to read into an array with read -a
:
$ read -a arr -p "Enter words: "
Enter words: v1 v2 v3 v4 v5
$ echo "${arr[@]}"
v1 v2 v3 v4 v5
$ read -a arr -p "Enter words: "
Enter words: v1 v2 v3 v4 v5 v6 v7 v8 v9 v10
$ echo "${arr[@]}"
v1 v2 v3 v4 v5 v6 v7 v8 v9 v10
This uses shell word splitting and assigns the input to the array arr
, elements of which can then be accessed using ${arr[0]}
, ${arr[1]}
etc.
The main advantage is that the array holds exactly as many elements as you entered and you don't have to know in advance how many there will be.
Upvotes: 1
Reputation: 15461
v
is not part of the sequence. Try this :
read -p "ENTER 1-20 WORDS" v{1..20}
Upvotes: 2