如果你
curl https://www.toptal.com/developers/gitignore/api/python
您会按预期看到文件,并带有换行符。但如果我
set response (curl https://www.toptal.com/developers/gitignore/api/python)
echo $response
鱼中的换行符消失了。我看过鱼read
但是
url $gitignoreurlbase/python | read response # I have also tried read -d 'blah'
echo $response
只显示空白。
如何捕获多行输出?
答案1
set var (command)
用。。。来代替set var (command | string split0)
解释:
默认情况下,命令替换在换行符上拆分。 $response 变量是行列表的输出。这是记录在案
$ 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
幸运的是,补救措施也是如此
$ 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
string split0
请注意保留尾随换行符的区别。
如果您同意 $response 作为行列表,但您只想正确显示它:
printf "%s\n" $response
# or, with just a literal newline as the join string
string join "
" $respose