问题描述
我尝试从 a..x
声明一个变量 X
所有字符。在命令行(bash)的, a..x
替代工程W / O任何刻度。
$ {回声} a..x
A B C D E F G H I J K L M N 2 O p q - [R式T U,V,宽x
但通过 X = {a..x}
的结果 {a..x} 为字符串。只有
X = $(回声{} a..x)
和
现在的问题是:这是分配的适当的方式或做我必须做其他的事情。
?的主要目的是将序列分配给数组,例如,
磁盘=($(回声{} a..x))
解决方案 您也可以使用设置
(但请务必保存位置参数,如果你仍然需要它们):
集合{} a..x
X =$ @
对于数组,括号扩建工程直接:
磁盘=({} a..x)
I try to declare a variable x
with all chars from a..x
. On the command line (bash), substitution of a..x
works w/o any ticks.
$ echo {a..x}
a b c d e f g h i j k l m n o p q r s t u v w x
But assigning it to variable via x={a..x}
results in {a..x}
as string. Only x=$(echo {a..x})
works.
The question is: Is this the proper way of assignment or do I have to do other things?
The main aim is to assign the sequence to an array, e.g.,
disks=( $(echo {a..x}) )
解决方案 You can also use set
(but be sure to save positional parameters if you still need them):
set {a..x}
x="$@"
For arrays, brace expansion works directly:
disks=( {a..x} )
这篇关于如何运用Bash替代的变量声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!