You are using $domain_name
in single quotes. This is why the shell is not expanding the variable.
In this case, though, I would suggest doing it a slightly different way:
jq
provides safe ways to import raw data from the shell:
jq '.mazda |= $ARGS.positional' file.json --args car{00..11}"$domain_name"
This would insert the mazda
key with the values taken from the list after --args
on the command line. The --args
and the list of values should be last on the command line. The values will be JSON-encoded as by jq
if needs be. This is usually better than trying to create correct JSON yourself from data that may come from external sources (for example, if the data that you want to insert contains tabs or newlines or double quotes, jq
will encode these properly as JSON strings with --args
).
The list is generated using a brace expansion. Assuming you are using bash
release 4 or later, the numbers generated by {00..11}
would be properly zero-filled.
Note that domain_name
does not need to be an environment (exported) variable.
"car%02.0f$domain_name "
in single quotes. – muru Dec 02 '19 at 05:10echo '{"mazda": []}' | jq --argjson IDS '['"$(seq -s, -f'"car%02.0f'"$domain_name"'"' 11)"']' '.mazda |= $IDS'
=>{ "mazda": [ "car01_hyb.com", "car02_hyb.com", "car03_hyb.com", "car04_hyb.com", "car05_hyb.com", "car06_hyb.com", "car07_hyb.com", "car08_hyb.com", "car09_hyb.com", "car10_hyb.com", "car11_hyb.com" ] }
– muru Dec 02 '19 at 05:11