Search code examples
shellgithub-cli

use gh cli to create issues list


I am trying to create an array of opened issue numbers from GitHub. I am using the following command to pull all the opened issue numbers we have in a specific repo and label:

issueNumList=$(gh issue list -R <repo name> -l "label" | cut -f1)

the problem is that I am unable to read from issueNumList as a list (obviously). I tried making it into an array by running this command, but it gets only the first number:

IFS=' \n' read -ra issuesArray <<< "${issueNumList[0]}"

I tried changing this command a few times but I don't see any difference in the output. this is the output I get:

$ echo ${issueNumList[0]}
1684 1683 1681 1680 1679 1678 1677 1676 1675 1674 1673 1672 1671 1670 1669 1668 1667
$ echo ${issueNumList[@]}
1684 1683 1681 1680 1679 1678 1677 1676 1675 1674 1673 1672 1671 1670 1669 1668 1667
$ echo "${issueNumList[0]}"
1684
1683
1681
1680
1679
1678
1677
1676
1675
1674
1673
1672
1671
1670
1669
1668
1667
$ echo ${issuesArray[@]}
1684
$ echo ${issuesArray[0]}
1684
$ echo ${issuesArray[1]}

$ echo ${issuesArray[2]}

from these outputs, I understand that there is only one spot in the issueNumList and issuesArray and from the third output I think it means that there is \n at the end of each number.


Solution

  • how to turn the output of the first command into an array?

    issueNumList is not an array - no sense to use [0] on it.

    read reads up until a newline. Specify a delimiter to zero byte, then you can read a newline-separated list. Also '\n' is twocharacters \ and n, it's not a newline, you can use ANSI C quoting in $'\n' to get a newline.

    issueNumList=$(gh issue list -R <repo name> -l "label" | cut -f1)
    IFS=$' \n' read -d '' -r -a issuesArray <<<"$issueNumList"
    

    but the way to read lines in bash into an array is readarray:

    readarray -t issuesArray <<<"$issueNumList"
    

    To debug variable contents use declare -p, like declare -p issuesArray or declare -p issueNumList.