jan
jan

Reputation: 2830

How to quote arguments with space in a variable inside another variable?

I have a script where I need to calculate complicated arguments and they could contain spaces. I need to store arguments in variable var1 and this variable and other arguments inside variable var2. I pass var2 as arguments to another script.

Example:

$ var1="2 3 4"
$ var2="1 \"$var1\" 5"
$ ./sript.sh $var2

Now "2 3 4" is not one argument to the script, but 3 ones: "2, 3 and 4". What I need is what happens if I call ./sript.sh 1 "2 3 4" 5, but with variable inside variable...

I tried with all mask and quote tricks I know, but it is not working.

Upvotes: 0

Views: 37

Answers (1)

chepner
chepner

Reputation: 531165

You don't; you use an array.

$ var1="2 3 4"
$ var2=(1 "$var1" 5)
$ ./script.sh "${var2[@]}"

Upvotes: 3

Related Questions