相关疑难解决方法(0)

如何在序列的 shell 括号扩展中使用 $variable?

我想$var in在 bash 中使用带有范围的 shell 括号扩展。简单地放置{$var1..$var2}不起作用,所以我去了“横向”......

以下工作,但它有点笨拙。

# remove the split files
echo rm foo.{$ext0..$extN} rm-segments > rm-segments
source rm-segments
Run Code Online (Sandbox Code Playgroud)

有没有更“正常”的方式?

shell bash brace-expansion

46
推荐指数
2
解决办法
2万
查看次数

我可以在没有 `eval` 的情况下在 {} 扩展中使用变量吗?

我可以在{}扩展中使用变量而不吸引eval吗?如果是这样,如何?

这不起作用:

$ touch 1.foo 1.bar
$ ls 1.{foo,bar}
1.bar  1.foo
$ extensions=foo,bar
$ ls 1.{$extensions}
ls: cannot access 1.{foo,bar}: No such file or directory
Run Code Online (Sandbox Code Playgroud)

它适用于eval

$ eval ls 1.{$extensions}
1.bar  1.foo
Run Code Online (Sandbox Code Playgroud)

bash wildcards brace-expansion variable-substitution

7
推荐指数
1
解决办法
1535
查看次数

使用通配符扩展 item={one,two,three}

如果我使用bash's 大括号扩展,我会得到一个列表

echo item={one,two,three}
item=one item=two item=three
Run Code Online (Sandbox Code Playgroud)

假设我在一个包含与通配符匹配的文件/文件夹的目录中,有没有办法让扩展与这些文件/文件夹匹配?

ls
blue  green  red

echo item=*      # Obviously not
item=*

echo item={*}    # Maybe? ...but no
item={*}
Run Code Online (Sandbox Code Playgroud)

在我的例子中,我希望扩展是 item=blue item=green item=red

我得到的最好的就是这样的代码

items=()
for dirent in *; do items+=("item=$dirent"); done
echo "${items[@]}"

item=blue item=green item=red
Run Code Online (Sandbox Code Playgroud)

bash wildcards

6
推荐指数
1
解决办法
314
查看次数