在变量扩展后添加文本 - 鱼

Adding text after variable expansion - Fish

我想在变量扩展后添加文本,但 fish 认为我指的是不同的变量名称,例如

$ set dessert "cake"
$ echo $dessert
cake
$ echo "I want 7 $desserts"
I want 7 
$ cat ./menu.txt | sed "s/$desserts/cookie/g"
sed: first RE may not be empty

您似乎想用大括号括起您的变量,例如“{$dessert}s”。 示例:

> set xyz blarch
> echo $xyz
blarch
> echo ${xyz}
Variables cannot be bracketed. In fish, please use {$xyz}.
fish: echo ${xyz}
            ^
> echo {$xyz}
blarch
> echo i like {$xyz}s
i like blarchs
> 

编辑 2:嗯...奖金鱼扩展 r.e。 @glenn 杰克曼的例子: 以这种方式排列以更容易地查看输出差异:

        example fish command                     ::    result
-------------------------------------------------::------------------------
> set foo bar; echo shakespere was a {$foo}      :: shakespere was a bar
> set foo bar; echo shakespere was a {$foo}d     :: shakespere was a bard
> set foo bar; echo "shakespere was a {$foo}d"   :: shakespere was a {bar}d
> set foo bar; echo "shakespere was a {"$foo"}d" :: shakespere was a {bar}d
> set foo bar; echo "shakespere was a "{$foo}"d" :: shakespere was a bard
> set foo bar; echo "shakespere was a "$foo"d"   :: shakespere was a bard
> 

看来最后一个效率最高

编辑:我想指出 Fish 的扩展 w/var 与其他外壳(如 bash)有些不同。请注意行为,{$xyz} 不会 失败 ,但它的工作方式与 Fish 不同......相反,{ } 在那里被视为文字并且它产生这样的“{moreblarch}”:

bash 示例:

$ export xyz=moreblarch
$ echo $xyz
moreblarch
$ echo {$xyz}
{moreblarch}
$ echo ${xyz}
moreblarch
$ echo who doesnt like ${xyz}s?
who doesnt like moreblarchs?
$ 

编辑 3:根据@glennjackman 的数组评论进行鱼排列 这不是我所期望的:-) "echo -e" 会将 \n 解释为换行符,这样更容易看到输出。请注意,排列在它们之间插入了 space(这就是为什么 #2 - #27 具有前导 space)。

> set a A B C; set b 1 2 3; set c x y z; echo -e ":a="$a" b="$b" c="$c":\n"
:a=A b=1 c=x:
 :a=B b=1 c=x:
 :a=C b=1 c=x:
 :a=A b=2 c=x:
 :a=B b=2 c=x:
 :a=C b=2 c=x:
 :a=A b=3 c=x:
 :a=B b=3 c=x:
 :a=C b=3 c=x:
 :a=A b=1 c=y:
 :a=B b=1 c=y:
 :a=C b=1 c=y:
 :a=A b=2 c=y:
 :a=B b=2 c=y:
 :a=C b=2 c=y:
 :a=A b=3 c=y:
 :a=B b=3 c=y:
 :a=C b=3 c=y:
 :a=A b=1 c=z:
 :a=B b=1 c=z:
 :a=C b=1 c=z:
 :a=A b=2 c=z:
 :a=B b=2 c=z:
 :a=C b=2 c=z:
 :a=A b=3 c=z:
 :a=B b=3 c=z:
 :a=C b=3 c=z:

>