Search code examples
bashvariablesimagemagick-convert

Using bash variable in text output


I'm using convert to add text to some images via a bash script. My issue is that I can't figure out how to add the contents of the variable to the text string to be placed in the image.

My script is as follows:

#! /bin/bash
for i in {1..10}
do
    convert -font TlwgTypewriter-Bold -pointsize 18 -fill white -draw 'text 140,29 "ID: $i"' $i.png $i-reward.png
done

But when I run it I get the following:

bashimage

How might I get the variable to output its contents into this string?


Solution

  • It seems it is due to wrong quoting of your command here:

    -draw 'text 140,29 "ID: $i"'
    

    Change that to:

    -draw "text 140,29 \"ID: $i\""
    

    It is because shell doesn't expand variables inside single quote.