bash命令输出
问题描述:
我这样做:bash命令输出
VAL=$(wc -l < file_with_5_lines)
for i in {1..${VAL}}; do echo $i; done
期待这样的结果:
1
2
3
4
5
的是相反,我得到这个:
{1..5}
编辑
此问题被标记为重复,但其他问题的接受答案在我看来无效。提出的解决方案是这样的:
VAL=$(wc -l < file_with_5_lines)
for i in {1..$((VAL))}; do
echo $i
done
,并继续给我这样的结果:
{1..5}
相反的:
1
2
3
4
5
答
在param之前完成Brace扩展eter扩展,这就是为什么我们不能在{...}
结构中有一个变量。使用for循环有规律,让你不依赖于外部命令状seq
:
for ((i = 1; i <= VAL; i++)); do
# your code here
done
+0
这是最好的解决方案,没有外部命令 – Sergio
只写一个正常的数字'for'循环。 –