是否可以在不使用eval的情况下使用变量绑定进行bash括号扩展? [英] Is it possible to do a bash brace expansion using a variable bound without using eval?
问题描述
请考虑以下脚本.
#!/bin/bash
echo {00..99}
n=99
echo {00..$n}
此脚本的输出为:
00 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
{00..99}
所需的输出是:
00 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
00 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
产生所需输出的一种解决方案是
One solution which produces the desired output is
eval echo {00..$n}
不幸的是,该解决方案使用了eval
,我希望尽可能避免使用.
Unfortunately, this solution uses eval
which I'd prefer to avoid if possible.
有人知道使用括号扩展而不是eval
来获得所需结果的方法吗?
Does anyone know of a way to obtain the desired result using brace expansion but not eval
?
推荐答案
来自bash手册:
扩展顺序为:大括号扩展;波浪线扩展,参数和变量扩展,算术扩展和命令替换(以从左到右的方式完成);分词和路径名扩展.
The order of expansions is: brace expansion; tilde expansion, parameter and variable expansion, arithmetic expansion, and command substitution (done in a left-to-right fashion); word splitting; and pathname expansion.
鉴于变量扩展是在大括号扩展之后进行的,并且不使用eval
就无法诱导不同的操作顺序,我不得不得出结论,不,没有避免使用eval
的方法.
Given that variable expansion comes after brace expansion, and that there is no way to induce a different order of operations without using eval
, I would have to conclude that no, there is no way to avoid using eval
.
这篇关于是否可以在不使用eval的情况下使用变量绑定进行bash括号扩展?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!