Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jq - concatenate passed arguments with strings

Tags:

json

bash

jq

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"
}
like image 527
alvgarvilla Avatar asked Sep 11 '25 14:09

alvgarvilla


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

like image 111
ikegami Avatar answered Sep 13 '25 04:09

ikegami