0

I'm trying to code something that looks like that:

#!/bin/bash
echo "type your separated words"
read *all variables*
echo *all variables*

Is that possible?

0

2 Answers 2

5

You could store them into an array using read

read -p $'type your separated words:\n' -a arr
printf "%s\n" "${arr[@]}"
Sign up to request clarification or add additional context in comments.

4 Comments

Array is the only way to store multiple unknown vars?
@ItaiGanot, for an unknown number of vars, this is the only solution I am aware of
Can you kindly explain what is "%s\n" "${arr[@]}" ?
@ItaiGanot %s format the array content as a string and ${arr[@]} corresponds to all of the array elements.
2

Another solution :

$ read
str1 str2 str3
$ set -- $REPLY
$ echo "$1"
str1
$ echo "$2"
str2
$ echo "$@"
str1 str2 str3

But I prefer the 1_CR solution.

1 Comment

+1 This will work in POSIX shells without array support, as well.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.