Fish command substitution doesn't work like in bash or zsh

淺唱寂寞╮ 提交于 2019-12-05 10:23:08

The relevant difference is called "word splitting," which is how the result of a variable expansion or command substitution is turned into multiple arguments.

In bash and zsh, word splitting occurs on all whitespace. Example:

> for i in $(echo 1 2 3) ; do echo $i; done
1
2
3

In fish, word splitting occurs only on newlines:

> for i in (echo 1 2 3); echo $i; end
1 2 3

In the above, the loop only runs once, with $i set to '1 2 3'. The advantage of the fish behavior is that filenames with spaces, etc. don't cause problems like they do in bash.

pkg-config outputs space-separated text:

> pkg-config --libs --cflags libcurl libssl
-lcurl -lssl -lcrypto -lz

So it's relying on bash's word splitting behavior. (But you'd be in trouble if any flags needed embedded whitespace.)

To get the same effect in fish, you can replace spaces with newlines. tr is a good tool for that:

pkg-config --libs --cflags libcurl libssl | tr -s ' ' \n

The -s flag effectively cleans up a trailing space that pig-config outputs.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!