0

Example here:

gitrepo=$(jq -r '.gitrepo' 0.json)
releasetag=$(curl --silent ""https://api.github.com/repos/\"$gitrepo\""/releases/latest" | grep '"tag_name":' | sed -E 's/.*"([^"]+)".*/\1/')
echo "$releasetag"

Used \" to escape characters.

0.json:

{
        "type": "github-releases",
        "gitrepo": "ipfs/go-ipfs"
}

How to put $gitrepo to work inside $releasetag? Thanks in advance!

1 Answer 1

2

Bash variables expand inside quoted " strings.

gitrepo="$(jq -r '.gitrepo' 0.json)"
releasetag="$(
  curl --silent "https://api.github.com/repos/$gitrepo/releases/latest" \
  | grep '"tag_name":' | sed -E 's/.*"([^"]+)".*/\1/'
)"
echo "$releasetag"

Btw, as you are using jq to extract .gitrepo from 0.json, you could also use it in the exact same way to extract .tag_name from curl's output (instead of using grep and sed) like so:

gitrepo="$(jq -r '.gitrepo' 0.json)"
releasetag="$(
  curl --silent "https://api.github.com/repos/$gitrepo/releases/latest" \
  | jq -r '.tag_name'
)"
echo "$releasetag"

And to simplify it even further (depending on your use case), just write:

curl --silent "https://api.github.com/repos/$(jq -r '.gitrepo' 0.json)/releases/latest" \
| jq -r '.tag_name'
Sign up to request clarification or add additional context in comments.

2 Comments

Many thanks! Do you have a GitHub acc? I would like to add thanks
No, I haven't but I appreciate it. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.