如何不将命令替换扩展为多个参数

How to not expand a command-substitution into multiple parameters

所以根据 to the docs 写在多行上的命令将扩展为多个参数。这与 bash 行为不一致。例如

hello.lua:

local msg = "Hello, world!" 
return msg

鱼>

redis-cli EVAL (cat hello.lua) 0

失败 -

redis-cli EVAL "$(cat hello.lua)" 0 将在 bash 中成功。我的问题是如何防止 (cat hello.lua) 替换因换行而分裂成多个参数?

fish 没有直接模拟 bash 的“$(...)”。 fish 目前最好的技术是操纵 $IFS,这是触发分裂的字符。您可以将其设为空:

set -l IFS
redis-cli EVAL (cat hello.lua) 0

这会将 hello.lua 的全部内容作为单个参数传递。

假设您不希望 IFS 更改持续存在,您可以将更改范围限定为函数或块:

begin
  set -l IFS
  redis-cli EVAL (cat hello.lua) 0
end