Shaihi
Shaihi

Reputation: 3974

Expand a variable inside `` command

I could not find the exact reference to what I'm doing...

I have the following script that does not expand the variable inside the command:

#!/bin/bash

name="my name"
`convert -pointsize 250 -font /usr/share/fonts/truetype/msttcorefonts/impact.ttf -fill black -draw 'text 330,900 "$name"' tag.jpg name_my.jpg`

This results in an image that has the text $name instead of the content of name.

I actually need to read lines from a file and rund the command on each name so my real script is(has the same problem):

arr=(`cat names.txt`)
for (( i=0; i<${len}; i+=2 ));
do
        `convert -pointsize 250 -font /usr/share/fonts/truetype/msttcorefonts/impact.ttf -fill black -draw 'text 330,900 "$(${arr[i]} ${arr[i+1]})"' tag.jpg name_${arr[i]}.jpg`
done

Upvotes: 0

Views: 150

Answers (2)

uml&#228;ute
uml&#228;ute

Reputation: 31274

you have an escaping problem. either use proper escaping with backslash, or make sure otherewise that the $args are not "protected" by single quotes. e.g.

name="bla"
# using escape character \
value1="foo \"${name}\""
# putting single-quotes inside double-quotes
value2="foo '"${name}"'"

to better see what is going on, try to break down the problem into multiple smaller problems. e.g. create the "draw" command with all expansions before using it in convert

name="my name"
draw="text 330, 900 '"${name}"'"
convert -pointsize 250 -fill black -draw "${draw}" tag.jpg name_my.jpg

Upvotes: 1

FatalError
FatalError

Reputation: 54551

Your problem is single quotes ('') not backticks. Because $name is within them, it won't be expanded. Instead, you should use double quotes and you can escape the inner quotes like this:

`convert -pointsize 250 -font /usr/share/fonts/truetype/msttcorefonts/impact.ttf -fill black -draw "text 330,900 \"$name\"" tag.jpg name_my.jpg`

Upvotes: 2

Related Questions