在 for 循环和 if 语句中回显过滤后的数字

在 for 循环和 if 语句中回显过滤后的数字

期望的输出:所有大于 5 的数字。

为数不多的尝试之一:

for i in {1..10}; do if ["$i" > 5]; then echo $i; fi; done

但输出是:

-bash: [1: command not found
-bash: [2: command not found
-bash: [3: command not found
-bash: [4: command not found
-bash: [5: command not found
-bash: [6: command not found
-bash: [7: command not found
-bash: [8: command not found
-bash: [9: command not found
-bash: [10: command not found

缺什么?

答案1

空间和-gt

user1@machine:~/tmp$ for i in {1..10}; do if  [ $i -gt 5 ]; then echo $i; fi; done
6
7
8
9
10

答案2

i=0
while case $((i+=1)) in
      ([6-9])
          echo "$i";;
      (??)
          ! echo "$i"
      esac
do :; done

答案3

如果您严格处理整数,那么您也可以使用bash 算术扩展:

$ for i in {1..10}; do if ((i>5)); then echo $i; fi; done
6
7
8
9
10
$ 

相关内容