I have been trying to create a string with a variable number of characters in it for a benchmark program. I am trying to use this to set up my messages string:
messages=`perl -e 'print "X" x $size'`
The goal is to create a dynamic number of characters. For example, if $size is 1 then there will be one X. If size is 5 then messages will be XXXXX. This doesn’t seem to work and the output is blank.
Am I misunderstanding something?
The reason that
$sizeis not evaluated by bash is that you enclosed it in single quotation marks ('). This gets more obvious if you use the$(...)syntax instead of the backticks:The principle is that everything inside single quotes is not touched by bash (only the quotes are stripped finally after word splitting), while things in double quotes get the various shell expansions (without quotes even more).
Thus Perl here gets
$sizeand can only try to evaluate this as a perl variable.As already said by other Diego, swapping the quotation signs around can help: