I have a variable with space separated list of paths. I need to send these paths to a program as a parameter like this script.sh -i /dir1 -i /dir2
. What is the best way to create such parameter list? Something like $(echo "$paths" | sed 's|\([^[:space:]]\+\)|-i \1|g)
will work but it's unreadable given the fact that it's part of the makefile.
Asked
Active
Viewed 180 times
0

synapse
- 499
- 2
- 6
- 14
-
1this question should be moved to [stackoverflow](http://www.stackoverflow.com) – mate64 Oct 18 '11 at 11:20
2 Answers
1
list="one two three"
for i in $list; do params="$params -i $i"; done
script.sh $params
You will have troubles with paths with spaces, using :
as paths separator is better

Selivanov Pavel
- 2,206
- 3
- 26
- 48
0
use bash arrays
paths="/dir1 /dir2"
params=()
for path in $paths; do params+=( -i $path ); done
script.sh "${params[@]}"

glenn jackman
- 4,630
- 1
- 17
- 20
-
You're correctly quoting the last expansion but none of the others; that will cause problems with spaces in filenames. `for path in "$paths"; do params+=( -i "$path" ); done` – adaptr Oct 18 '11 at 14:40
-
No. "I have a variable with space separated list of paths." Therefore, you must not quote `$paths` in the for statement, and you're guaranteed that `$path` will not have a space in it. – glenn jackman Oct 18 '11 at 15:21
-
Ugh, you're right. Brainfart on the silliness of the original question. – adaptr Oct 18 '11 at 15:46