0

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.

Killerz0ne
  • 254
  • 1
  • 2
  • 12

1 Answers1

1

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.

KamilCuk
  • 120,984
  • 8
  • 59
  • 111
  • which package has readarray? brew doesn't find it – Killerz0ne May 19 '21 at 11:10
  • bash >4.something has readarray, macos has very old bash – KamilCuk May 19 '21 at 11:12
  • unfortunately that's what we use on our images – Killerz0ne May 19 '21 at 11:15
  • You can always `tr '\n' ' '` or `paste -sd' '` and read space-separated list, or just ignore best -practice and use word splitting. – KamilCuk May 19 '21 at 11:26
  • just for future reference, what @KamilCuk offered worked perfectly on my mac, but on the debian image (version stretch-slim) it didn't because of the ```-d``` (even though it's an option). had to remake the 2 lines into: ```issueNumList=$(gh issue list -R -l "label" | cut -f1 | tr '\n' ',') IFS=$' ,' read -r -a issuesArray <<< "$issueNumList"``` – Killerz0ne Jun 02 '21 at 06:31