Reputation: 179
Is there a way to build a varible like this:
var="-i \"/path to/file 1.mp4\" -i \"/path to/file 2.mp4\""
And then use this later in a program:
ffmpeg $var
I know \"
doesn't work, and eval
works in a way, but eval can be ugly.
Is there a solution without eval?
Upvotes: 0
Views: 75
Reputation: 1577
The next best thing to do is to build an array. When you put ${var[@]}
somewhere, it will replace it with the complete array contents. If you put "${var[@]}"
somewhere (with quotes), the elements of the array will be quoted, so elements with spaces will not be split at the whitespace into multiple elements:
#!/bin/bash
function test() {
for ((i=0; i<$#; i++)) {
echo "Param $i: ${!i}"
}
}
var[0]="-i"
var[1]="/a/path"
var[2]="-i"
var[3]="/second/path with space/"
test "${var[@]}"
Will output:
Param 0: test.sh
Param 1: -i
Param 2: /a/path
Param 3: -i
and that is exactly what you require. I only tried this in bash, other shells may behave differently.
Upvotes: 1