How to code a parameter with a variable that expands into a variable number of parameter components

40 views Asked by At

This is a very basic question about parameter expansion. By way of example, here are two forms of the audio codec parameter for the ffmpeg command:

Form 1:

ffmpeg ... -acodec copy ...

Form 2:

ffmpeg ... -an ...

I would like to express the parameter and (for form 1) its value as a variable or variables that would work in both scenarios.

This works for form 1:

param1="-acodec"
param2="copy"
ffmpeg ... "$param1" "$param2" ...

but this doesn't work:

param="-acodec copy"
ffmpeg ... "$param" ...

nor does this work:

param1="-acodec"
param2=" copy"
ffmpeg ... "$param1$param2"

Conversely, this works for form 2:

param="-an"
ffmpeg ... "$param" ...

and this works:

param1="-an"
param2=""
ffmpeg ... "$param1$param2"

but this obviously doesn't work because of the extra space character:

param1="-an"
param2=""
ffmpeg ... "$param1" "$param2" ...

Is there a single expression that works for both forms of the parameter?

1

There are 1 answers

2
chepner On BEST ANSWER

Use an array.

# Pick one.
# param=(-acodec copy)
# param=(-an)

ffmpeg ... "${param[@]}" ...