我已经挖了一个答案,而what I've found似乎没有用。如果我运行weather "NYC"
,echo'd行是“weather = NYC”,但是如果我运行weather
,则echo'd行是“weather =”。如何为$location
设置默认值?
function weather --argument location --description "Display weather information for a given location"
set -q location; or set location "San Francisco, CA"
echo "weather = $location"
curl -H "Accept-Language: en" "wttr.in/$location?u"
end
更新
我已经能够通过以下更改获得所需的行为,但如果set -q location; or set location "San Francisco, CA"
应该有效,我仍然很好奇。它似乎在命令行上,但不在函数中。
if not test -n "$location"
set location "Washington, DC"
end
答案 0 :(得分:3)
请参阅https://github.com/fish-shell/fish-shell/issues/2645
你的解决方法的可疑解决方案是:
set -q location[1]
or set location "Washington, DC"
在实践中,您的解决方案和上面的可疑方法等同于简单地测试是否设置了var。