Building Parameters Programmatically
With Arrays
Created: 2021-02-13
Updated: 2021-04-29
Some programs, like ffmpeg, can take a lot of command line parameters.
I had written a
As I found out, when we use a variable in bash like this:
FORMAT='-qscale:v 2 -f rtsp'```
When we call it, it is interpreted like this:
```bash
-qscale:v (2, -f, rtsp)```
```bash
-qscale:v is passed 2, -f, and rtsp```
Thanks to this post for explaining to use arrays.
So to get the correct behavior
PARAMS=(-qscale:v)
PARAMS+=(2)
PARAMS+=(-f)
PARAMS+=(rtsp)
Or Simply
PARAMS=(-qscale:v 2 -f rtsp)
And to 'unpack':
"${PARAMS[@]}"
Which I found very similar to Python equivalent of:
PARAMS=['-qscale:v', '2', '-f', 'rtsp']
subprocess.Popen(['ffmpeg', *PARAMS])