6

I'm getting a curl command output as below

curl -s https://api.github.com/repos/harshavardhanc/dockerfile-ansible/pulls\?state\=all | jq -r '.[]|(.number|tostring)+" "+.user.login+" "+.created_at+" "+.merged_at'
5 test 2019-09-27T11:06:23Z 2019-09-27T11:09:28Z
4 test1 2019-09-26T16:56:40Z 2019-09-26T16:57:02Z
3 test2 2019-09-26T16:54:25Z 2019-09-26T16:54:55Z
2 test3 2019-09-26T16:52:59Z 2019-09-26T16:55:19Z
1 test4 2019-09-26T16:46:52Z 2019-09-26T16:47:25Z

and I'm storing it in a variable and trying to pass echo the variable to pass the value to another command to parse the output. But when I'm trying to echo the variable it is printing all the lines as a single line.

prlist=$(curl -s https://api.github.com/repos/harshavardhanc/dockerfile-ansible/pulls\?state\=all | jq -r '.[]|(.number|tostring)+" "+.user.login+" "+.created_at+" "+.merged_at')
echo $prlist
5 test 2019-09-27T11:06:23Z 2019-09-27T11:09:28Z 4 test1 2019-09-26T16:56:40Z 2019-09-26T16:57:02Z 3 test2 2019-09-26T16:54:25Z 2019-09-26T16:54:55Z 2 test3 2019-09-26T16:52:59Z 2019-09-26T16:55:19Z1 test4 2019-09-26T16:46:52Z 2019-09-26T16:47:25Z

How to avoid this printing in different line? Please help.

  • 1
    Please edit your question and show us the exact commands you are using. We need to see the curl command and the echo command. You're probably simply not quoting correctly, but you almost certainly don't need to echo at all. If you give us the full commands, we can give you a better solution. – terdon Sep 28 '19 at 12:47
  • @terdon I have updated the question with the curl command I'm using. – harsha Sep 28 '19 at 12:52
  • Thank you, but as I said before, we need all commands, including the echo one. In fact, especially the echo. How are you saving the curl output in a variable? How are you then printing that variable? And what parsing are you trying to do? – terdon Sep 28 '19 at 12:55
  • @dessert I have updated it – harsha Sep 28 '19 at 12:58
8

What you describe is the standard behavior of echoing an unquoted variable:

$ prlist=$(curl -s https://api.github.com/repos/harshavardhanc/dockerfile-ansible/pulls\?state\=all | 
           jq -r '.[]|(.number|tostring)+" "+.user.login+" "+.created_at+" "+.merged_at')
$ echo $prlist
5 SMYALTAMASH 2019-09-27T11:06:23Z 2019-09-27T11:09:28Z 4 ganesh-28 2019-09-26T16:56:40Z 2019-09-26T16:57:02Z 3 ganesh-28 2019-09-26T16:54:25Z 2019-09-26T16:54:55Z 2 ganesh-28 2019-09-26T16:52:59Z 2019-09-26T16:55:19Z 1 ganesh-28 2019-09-26T16:46:52Z 2019-09-26T16:47:25Z

That's because $prlist is not quoted. Compare with what happens if you quote it:

$ echo "$prlist"
5 SMYALTAMASH 2019-09-27T11:06:23Z 2019-09-27T11:09:28Z
4 ganesh-28 2019-09-26T16:56:40Z 2019-09-26T16:57:02Z
3 ganesh-28 2019-09-26T16:54:25Z 2019-09-26T16:54:55Z
2 ganesh-28 2019-09-26T16:52:59Z 2019-09-26T16:55:19Z
1 ganesh-28 2019-09-26T16:46:52Z 2019-09-26T16:47:25Z

But why use a variable at all? You can just parse the output of curl directly:

curl ... | jq ... | someOtherCommand

Like this:

curl -s https://api.github.com/repos/harshavardhanc/dockerfile-ansible/pulls\?state\=all | 
  jq -r '.[]|(.number|tostring)+" \
    "+.user.login+" "+.created_at+" "+.merged_at'curl \
    -s https://api.github.com/repos/harshavardhanc/dockerfile-ansible/pulls\?state\=all |
 someOtherCommand
  • 1
    Thanks @terdon, adding quotes worked. – harsha Sep 28 '19 at 13:01
  • I'm storing it in a variable because I need to pass same output as input to multiple api's down the script. @terdon – harsha Sep 28 '19 at 17:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.