Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a variable with space separated list of paths. I need to send these paths to a program as a parameter like this -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.

share|improve this question
this question should be moved to stackoverflow – mate64 Oct 18 '11 at 11:20
up vote 1 down vote accepted
list="one two three"
for i in $list; do params="$params -i $i"; done $params

You will have troubles with paths with spaces, using : as paths separator is better

share|improve this answer

use bash arrays

paths="/dir1 /dir2"
for path in $paths; do params+=( -i $path ); done "${params[@]}"
share|improve this answer
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

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.