jq - concatenate passed arguments with strings

Viewed 686

I'm able to use arguments if they are not surrounded by quotes:

var=$(jq -n --arg hostname "my-hostname" '{
  Name: $hostname
}
)

echo $var

Result:

{
  Name: "my-hostname"
}

But I want to concatenate the variable with an existing string, it ignores the value:

var=$(jq -n --arg hostname "my-hostname" '{
  Name: "prefix-value-$hostname"
}
)

echo $var

Result:

{
  Name: "prefix-value-"
}

Expected:

{
  Name: "prefix-value-my-hostname"
}
1 Answers

Replace

"prefix-value-$hostname"

with

"prefix-value-\( $hostname )"

jqplay

or

"prefix-value-" + $hostname

jqplay


Note that since host names can't contain line feeds, -n and --arg could be replaced with -R and stdin.

echo my-hostname | jq -R '{ Name: "prefix-value-\(.)" }'

jqplay

Related