Search code examples
bashfor-loopparameter-passingimagemagick-convert

command line arguments fed from an array


I'd like to perform a complicated bash command, using data fed from a long array as arguments. I guess it has to use a subshell in some way.

For example, instead of the workable convert -size 100x100 xc:black -fill white -draw "point 1,1" -draw "point 4,8" -draw "point 87,34" etc etc etc image.png

I want to employ a different logic where the arguments are given in the same command, more like

convert -size 100x100 xc:black -fill white $(for i in 1,1 4,8 87,34 etc etc; -draw "point $i"; done) image.png which doesn't work, as the $i is interpreted as a command insted of an argument.

Please note that "for i in ...; do convert ...$i...; done" will not work. The -draw "point x,y" series of arguments must be in the same single run convert command, as convert doesn't accept -draw parameter in existing images.


Solution

  • Build up an array of -draw arguments first.

    for pt in 1,1 4,8 87,34; do
        points+=(-draw "point $pt")
    done
    convert -size 100x100 xc:black -fill white "${points[@]}" image.png