1

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.

0

2 Answers 2

2

v is not part of the sequence. Try this :

read -p "ENTER 1-20 WORDS" v{1..20}
Sign up to request clarification or add additional context in comments.

Comments

1

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.

5 Comments

Could you expand on this a little? I'm having trouble understanding. For instance, supposing I wanted to use grep to search for multiple strings entered without having any predetermined notion of what those strings will be or the number of strings that will be entered. If needed I can update the original question.
@user556068 It depends a bit on what you'd like to grep: all terms, or any of them. This question asks about creating a grep command from multiple search terms. Or what exactly is it you don't quite understand?
"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." So if I wanted to grep any and/or all of an undetermined number of user defined terms using the array variable, how would I go about doing that? For instance, On run 1: I want to grep 3 random terms. On run 2: I want to grep a different set of 7 random terms. On run 3: I want to grep 4 random terms.
Its not so much the grep command i'm interested in. Understanding the array variables and how to apply them in a real world situation is the main thing. Grep is just an example command.
@user556068 This is a pretty good overview.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.