Jump to next element in a loop

Hi, i want the loop to move to 3rd item, when it encounters 2 while runing

for i in 1 2 3 4 5 6
do
if [ $i -eq 2 ]
then
#jump to element 3
else
echo $i
fi
done

o/p

1
3
4
5
6

can anyone help, how this can be implemented ?

There isn't a command to break out an if statement, the simplest solution should be:

if [ "$i" -ne 2 ]

Or use "continue":

for i in 1 2 3 4 5 6
do
  if [ $i -eq 2 ]
  then
    continue #jump to element 3
  else
    echo $i
  fi
done
1 Like

Sure, it's an if statement within a for loop... time for coffee... :eek:

thanks,
i forgot the use of continue,
i was thinking it would make the loop start from 1st again..... my bad.. sorry for spoiling your time.