1

Is it possible to invoke some program in a Bash script with complete command line parameters (both the key and the value) stored in variables?

I use the following scanimage call in a script:

scanimage -p --mode "True Gray" --resolution 150 -l 0 -t 0 -x 210 -y 297 --format=png  -o scan.png

I want to store some of the parameters in variables. I tried this, concerning the --mode switch:

options="--mode \"True Gray\""
scanimage -p $options --resolution 150 -l 0 -t 0 -x 210 -y 297 --format=png  -o scan.png

but this doesn't work, scanimage says:

scanimage: setting of option --mode failed (Invalid argument)

Storing only the value for the --mode switch does work:

mode="True Gray"
scanimage -p --mode "$mode" --resolution 150 -l 0 -t 0 -x 210 -y 297 --format=png  -o scan.png

but I'd like to be variable about the switches, and I'd also like to customize multiple switches without knowing which will be set.

So is it possible to store not only the values for command line options in a variable, but also the option switch(es) along with the value(s)?

1 Answers1

1

You can do this if you use an array instead of a string. Try this:

options=( '--mode' "True Gray" )
scanimage -p "${options[@]}" --resolution 150 -l 0 -t 0 -x 210 -y 297 --format=png  -o scan.png
terdon
  • 242,166