1

I have a script find-files that produces paths that can contain space in lines

/foo bar
/abc

I want to convert these paths to arguments that get to pass to another command cmd like this:

cmd -f "/foo bar" -f "/abc"

I tried this

cmd $(find-files | sed 's/^/-f /')

But it passes paths containing space as multiple arguments.

and if I quote the substitution, the whole string gets passed as a single argument.

What's the correct way of doing that?

BTW, this is not the same question as asked here, which suggested eval. eval doesn't help with dealing spaces at all.

0

2 Answers 2

2

Use a loop to build up the necessary array.

while IFS= read -r fname; do
  args+=(-f "$fname")
done < <(find-files)

cmd "${args[@]}"

This assumes that the file names cannot contain newlines, though, since read will treat such files as consecutive file names, not a single name. It's not clear if that is an issue for you, or if your find-files command can provide a safe alternative (such as output a null-separated list of file names, rather than lines).

Sign up to request clarification or add additional context in comments.

Comments

-1

Put all your filenames in an array.

lst=( "/foo bar" /abc ) # quote as necessary

Then use printf to insert the -f.

cmd $( printf ' -f "%s"' "${lst[@]}" ) # leading space helps

For example,

$: echo $( printf ' -f "%s"' "${lst[@]}" )
-f "/foo bar" -f "/abc"

The leading space in front of the -f simplifies a bit.
Without it you get:

$: echo $( printf '-f %s' "${lst[@]}" )
bash: printf: -f: invalid option
printf: usage: printf [-v var] format [arguments]


EDIT:

Added double quotes inside the printf format.

4 Comments

I don't think this is correct, what echo got is "-f" "/foo" "bar" "-f" "/abc", since the printf cmd itself isn't quoted.
Added double quotes inside the printf format. Try that.
Good start, but the unquoted command substitution takes you back to square one.
If you quote the substitution then the entire set of arguments get passed as a single arg.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.