bash循环除了给定数字之外的数字列表

and*_*beu 4 bash loops

循环遍历bash中的连续数字列表我能做到

for s in $(seq 1 5);do
   echo ${s}
done
Run Code Online (Sandbox Code Playgroud)

循环遍历一个连续的数字列表,在python中留下给定的数字,我可以这样做:

list = [s2 for s2 in range(6)[1:] if s2 != s1]
for s1 in list:
   print s1
Run Code Online (Sandbox Code Playgroud)

其中list包含除s1之外的所有数字

我如何在bash中做同样的事情?

fed*_*qui 6

只是continue用来跳过这一步:

for s in {1..5}                 # note there is no need to use $(seq...)
do
   [ "$s" -eq 3 ] && continue   # if var is for example 3, jump to next loop
   echo "$s"
done
Run Code Online (Sandbox Code Playgroud)

返回:

1
2
4             # <--- 3 is skipped
5
Run Code Online (Sandbox Code Playgroud)

Bash参考手册→4.1 Bourne Shell Builtins:

继续

continue [n]
Run Code Online (Sandbox Code Playgroud)

恢复封闭for,while,until或select循环的下一次迭代.如果提供n,则恢复执行第n个封闭循环.n必须大于或等于1.返回状态为零,除非n不大于或等于1.