0

I have been trying to find an example of bash usage where a preexisiting variable can be referenced within a $() command. I need to do this to assign a new variable to the output of the $() style command. I am not entirely sure how to search for what I am trying to achieve either. I am trying to use a variable 'vid' within a curl request. I'd love some help on this. Thanks! Here is an example of the bash:

vid= $(curl -X POST -H "Content-Type: application/json" -d @create_contact.json https://api.hubapi.com/contacts/v1/contact/?hapikey=$HUBSPOT_API_KEY | jq '.vid')
checkVid= $(curl -s https://api.hubapi.com/contacts/v1/contact/vid/"$vid"/profile?hapikey=$HUBSPOT_API_KEY | jq '.vid')
Evan Gertis
  • 1,796
  • 2
  • 25
  • 59
  • 5
    No space after =. – choroba Apr 13 '20 at 18:50
  • Does this help? https://unix.stackexchange.com/q/258727 – Quasímodo Apr 13 '20 at 18:51
  • 1
    ensure there's no extra spaces, and quote around the whole thing, not just the variable, e.g. `"https://api.hubapi.com/contacts/v1/contact/vid/$vid/profile?hapikey=$HUBSPOT_API_KEY"` – Adam Katz Apr 13 '20 at 18:51
  • 1
    @AdamKatz: Nothing in the OP's code leads me to think he needs to export the variables. Can you elaborate on why you recommend that? – ruakh Apr 13 '20 at 18:54
  • @AdamKatz, ...to be clear, unexported variables do make it to subshells, and `$(...)` is very much a subshell, not a regular subprocess (behind an `exec` boundary, through which only exported variables will survive). – Charles Duffy Apr 13 '20 at 18:55
  • @choroba thanks for that! I feel so foolish. I didn't know that was an issue. I am learning. It seems to work so long as I removed the space after the equals sign. Should I take the question down or is it still meaningful? – Evan Gertis Apr 13 '20 at 18:57
  • It's still meaningful, but perhaps a duplicate, as this is a common pitfall. – choroba Apr 13 '20 at 19:05

0 Answers0