我想创建 5 个同名但带有增量计数器的文件。
如果我做:
for i in {1..5}; do touch hallo_$i; done
我得到,,,hallo_1
等等。hallo_2
hallo_3
如果我做:
for i in {1..5}; do touch company-price-$i_spec.rb; done
我得到:company-price-.rb
我必须逃避什么吗?难道 bash 认为我想从$i
变量中删除/减去?
如果我逃跑了.
,-
我仍然会得到同样的结果。有什么提示吗?
答案1
您必须使用变量扩展的大括号形式:
for i in {1..5}; do touch company-price-${i}_spec.rb; done
否则,bash
将被视为$i_spec
变量扩展,而不是$i
.
从bash
手动的:
The basic form of parameter expansion is ${parameter}. The value of
parameter is substituted. The braces are required when parameter is a
positional parameter with more than one digit, or when parameter is
followed by a character that is not to be interpreted as part of its name.