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
    Run set -x in your terminal and then try the command again and post the output. That will show us exactly what is being passed to the scanimage command. I am guessing it will be '--mode' '"True' 'Gray"'.
    – terdon
    Feb 17, 2022 at 17:33
  • Yes, exactly! + scanimage -p --mode '"True' 'Gray"' ... Feb 17, 2022 at 17:35
  • 2
    Use an array, with separate elements for each option and its argument. See for example Pass a string or array as arguments in bash Feb 17, 2022 at 17:35

1 Answer 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
  • That did the trick. Thanks a lot :-) Feb 17, 2022 at 17:41

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .