How to print variable value in next line using echo command

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.


Solution 1:

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