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 
+2

只写一个正常的数字'for'循环。 –

在param之前完成Brace扩展eter扩展,这就是为什么我们不能在{...}结构中有一个变量。使用for循环有规律,让你不依赖于外部命令状seq

for ((i = 1; i <= VAL; i++)); do 
    # your code here 
done 
+0

这是最好的解决方案,没有外部命令 – Sergio

尝试下面的代码,

VAL=$(wc -l < file_with_5_lines) 
for i in `seq ${VAL}` 
do 
    echo $i 
done 
+0

这工作正常 – Sergio

VAL=$(wc -l < file_with_5_lines) 
for i in $(seq $VAL);do echo $i;done 
+1

这里有什么问题? – L30n1d45

+1

试试这里:https://www.jdoodle.com/test-bash-shell-script-online 'echo -e“line1 \ nline2 \ nline3 \ nline4 \ nline5”> input.txt; VAL = $(wc - l L30n1d45

+0

这工作正常 – Sergio