2

if you

curl https://www.toptal.com/developers/gitignore/api/python

you see the file as expected, with newlines. But if I

set response (curl https://www.toptal.com/developers/gitignore/api/python)
echo $response

in fish the newlines are gone. I've looked at fish read but

url $gitignoreurlbase/python | read response # I have also tried read -d 'blah'
echo $response

just shows a blank.

How do I capture the multi-line output?

1 Answer 1

4

Replace set var (command) with set var (command | string split0)

Explanation:

command substition splits on newlines by default. The $response variable is a list of lines of the output. This is documented

$ set var (seq 10)
$ set --show var
$var: not set in local scope
$var: set in global scope, unexported, with 10 elements
$var[1]: length=1 value=|1|
$var[2]: length=1 value=|2|
$var[3]: length=1 value=|3|
$var[4]: length=1 value=|4|
$var[5]: length=1 value=|5|
$var[6]: length=1 value=|6|
$var[7]: length=1 value=|7|
$var[8]: length=1 value=|8|
$var[9]: length=1 value=|9|
$var[10]: length=2 value=|10|
$var: not set in universal scope

Fortunately so is the remedy

$ set var (seq 10 | string split0)
$ set -S var
$var: not set in local scope
$var: set in global scope, unexported, with 1 elements
$var[1]: length=21 value=|1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n|
$var: not set in universal scope

# OR
$ set oldIFS $IFS
$ set --erase IFS
$ set var (seq 10)
$ set -S var
$var: not set in local scope
$var: set in global scope, unexported, with 1 elements
$var[1]: length=20 value=|1\n2\n3\n4\n5\n6\n7\n8\n9\n10|
$var: not set in universal scope
$ set IFS $oldIFS

Note the difference with the string split0 keeping the trailing newline.

If you're OK with $response being a list of lines, but you just want to display it properly:

printf "%s\n" $response

# or, with just a literal newline as the join string
string join "
" $respose
1
  • My apologies I didn't RTFM carefullly enough. The very short version of your answer would be “pipe through string split0 before capturing” ? Aug 11, 2020 at 14:35

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.