I want to pass bash variable in to curl request in bash script:

#request sshkey from do rebrain account
do_token=`echo "var.do-token" | terraform -chdir=/input console -var-file terraform.auto.tfvars`
echo $do_token
curl -X GET -s -H "Authorization: Bearer $do_token" -d '{ "job_run": {} }' "https://api.digitalocean.com/v2/account/keys?page=1" | jq -r --arg queryname "User's key" '.ssh_keys[] | select(.name == $queryname).public_key'

echo $do_token returns appropriate token in quotes like this: "token". But it throws an error "jq: error (at <stdin>:0): Cannot iterate over null (null)".

Then I passing it into main.tf:

data "external" "fetchssh" {
  program = ["bash", "${path.module}/get_sshkey.sh"]
  query = {
    do_token = var.do-token

to use data.external.fetchssh variable in terraform

How should I implement it to avoid error?

  • You're pointing out that echo $do_token outputs the token in quotes. Should the quotes really be part of the token?
    – Kusalananda
    Jun 10, 2021 at 18:24
  • @Kusalananda, well, when I manual puts token in quotes in curl, it works Jun 10, 2021 at 18:25
  • Note that the shell does quote removal as a step in the processing of the command line. So if you put quotes around the token on the command line, the quotes will not actually be part of the token. It's like the difference between echo "token", echo token, and echo '"token"'. Only with the last echo, token will be output with quotes around it (and the single quotes will have been removed). I seriously doubt that your token should actually contain quotes.
    – Kusalananda
    Jun 10, 2021 at 19:49
  • Quote the "'" using backslash "`": "User\'s key".
    – waltinator
    Jun 11, 2021 at 3:57
  • Always paste your script into https://shellcheck.net, a syntax checker, or install shellcheck locally. Make using shellcheck part of your development process.
    – waltinator
    Jun 11, 2021 at 3:58

1 Answer 1


This worked "${do_token//"}" so passing it into curl -X GET -s -H "Authorization: Bearer ${do_token//\"}" "https://api.digitalocean.com/v2/account/keys?page=1" | jq -r --arg queryname "User's key" '.ssh_keys[] | select(.name == $queryname).public_key' will return ssh key

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .