0

I know how to do sequential for loops like:

for i in $(seq 0 63)
do
  echo $i
done

This would print 0-63 . But what if I wanted certain numbers looped like only 0, 5, 25, 43, 44, 51, 54.

I know I could do the following:

for i in $(seq 0 63)
do
  if [ "$i" -eq 0 ] || [ "$i" -eq 5 ] || [ "$i" -eq 25 ] || [ "$i" -eq 43 ] || [ "$i" -eq 44 ] || [ "$i" -eq 51 ] || [ "$i" -eq 54 ]; then
    echo $i
  fi
done

But the result will be that it still goes through those loop iterations, and is not an efficient solution.

Is there something I can use with seq to describe these certain numbers 0,5,25,43,44,51,54?

0

1 Answer 1

1

If you want to iterate over a known list, you can simply store the elements, space-separated, in a variable and loop over that:

nums="0 5 25 43 44 51 54";
for i in $nums;
do echo $i;
done;
Sign up to request clarification or add additional context in comments.

1 Comment

For the OP, seq is just generating such a hard-coded list.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.