2

I have the following simple function, script and I would like to set variable $1 or anyother one inside the following:

curl --request POST \
  --url 'https://api.securitytrails.com/v1/domains/list?include_ips=false&scroll=false' \
  --header 'APIKEY: XXX' \
  --header 'Content-Type: application/json' \
  --data '
{
  "filter": {
    "whois_organization": "$1"
  }
}
'

I tried couple solutions in StackExchange related to inserting variable inside single quotes by using double quotes + escapes but did not work, I know that inside single quotes everything is preserved literally but the variable is inside JSON value have double quotes, I thought since double quotes exists then it will take the first argument of the script, function.

Any thoughts of solving this so I can pass the first argument in the script to the sent request ?

1 Answers1

3

Please see Parameter expansion (variable expansion) and quotes within quotes. In 'foo "$1" bar' everything is single-quoted, including double-quotes that have no special meaning.

You can concatenate strings quoted separately or unquoted. E.g. this:

"D quoted"'S quoted'not quoted"D quoted again"

will be treated by the shell as two words. The unquoted (and unescaped) space separates words and nothing happens where the quoting changes (I mean no space nor anything is inserted). The resulting words will be: D quotedS quotednot, quotedD quoted again.

Thus you can do this:

var='lorem ipsum'
echo 'the value of $var is '"$var"

where the type of quotes changes but the resulting string forms one word (i.e. one argument to echo). If you need literal double-quotes then single-quote them:

var='lorem ipsum'
echo 'the value of $var is "'"$var"'"'

I think this is what you want:

  --data '
{
  "filter": {
    "whois_organization": "'"$1"'"
  }
}
'

The fragment is like:

--data '… "'"$1"'" …'
#      ^   ^           a pair of single-quotes
#               ^   ^  another pair of single-quotes
#       ^^^      ^^^   single-quoted content
#           ^  ^       a pair of double-quotes
#            ^^        double-quoted parameter

Here only the positional parameter is double-quoted (so it will be expanded); the rest of the string is single-quoted.