Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between continue and colon in conditional statement

Tags:

bash

What is the difference between using a colon, which means "do nothing" and continue, which means skip.

if [[ -s $file ]] ; then 
    : 
fi 

if [[ -s $file ]] ; then 
   continue
fi 
like image 381
capser Avatar asked Mar 25 '26 02:03

capser


2 Answers

: is a synonym for true. It does not prevent later commands in the same block or loop from running.

Compare:

for (( i=0; i<3; i++ )); do
  echo "Starting iteration $i"
  (( i == 1 )) && { echo "  About to run :"; :; echo "  Just ran :"; }
  (( i == 2 )) && { echo "  About to run continue"; continue; echo "  Just ran continue"; }
  echo "Ending iteration $i"
done

Our output is:

Starting iteration 0
Ending iteration 0
Starting iteration 1
  About to run :
  Just ran :
Ending iteration 1
Starting iteration 2
  About to run continue

Note that we made it to "ending" after running :, but not after running continue.

like image 114
Charles Duffy Avatar answered Mar 28 '26 02:03

Charles Duffy


It depends on your program's logic.

Outside of a loop you get

$ continue
bash: continue: only meaningful in a `for', `while', or `until' loop
like image 39
glenn jackman Avatar answered Mar 28 '26 02:03

glenn jackman



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!