I'm trying to use the `convert` command from ImageMagick to insert text onto an image, and I'm stuck on how to include variables for the text. I want to replace a manual entry with a variable. For example, I have a variable `ln1` set to 'The first line.' and I want to use it like this:
```bash
ln1='The first line.' ; convert -pointsize 85 -fill white -draw 'text 40,100 ${ln1}' -draw 'text 40,200 "The second line."' source.jpg out.jpg
```
The issue is that single quotes seem to confuse the command, and I've tried various ways to make it work. According to the [help doc](http://www.graphicsmagick.org/GraphicsMagick.html#details-draw) about the `draw` operand, there's no clear example of how to do this. Also, I need to keep the `draw` function encapsulated in single quotes, but I'm not sure how to include variables correctly within that. Can anyone guide me on how to properly use the `ln1` variable? Thanks!
3 Answers
To use the variable correctly in your command, remember not to wrap it in single quotes at the point of use. If you want to include double quotes in your text, just escape them with a backslash. Here's a command that might work for you:
```bash
ln1='The first line.'; convert -pointsize 85 -fill white -draw "text 40,100 '${ln1}'" -draw "text 40,200 'The second line.'" source.jpg out.jpg
```
By enclosing your text in double quotes outside and keeping the variables inside single quotes, they should expand correctly!
You should use double quotes when working with variables in Bash. Single quotes treat everything inside as a literal string. So, if you want to use a variable like `${ln1}`, you need to rewrite your command using double quotes. An example would look like this:
```bash
ln1='The first line.'; convert -pointsize 85 -fill white -draw "text 40,100 '${ln1}'" -draw "text 40,200 'The second line.'" source.jpg out.jpg
```
This way, `${ln1}` will be evaluated and inserted correctly!
Thanks for clarifying! I think I was confused about the quotes. So I should avoid using single quotes with variable names then!
You're right about Bash not interpolating variables inside single quotes. You could also use a subshell to get around this limitation. For example:
```bash
export ln1="The first line."
convert -pointsize 85 -fill white -draw "$(echo 'text 40,100 "${ln1}"')" -draw 'text 40,200 "The second line."' source.jpg out.jpg
```
This method allows you to call the `ln1` variable properly by using double quotes on the outside.

Got it! I see how that works now. Thanks for the example!