Skip to content
TrySitePrice

Unix: csh Shell Loop Example

Best/Tutorial

In the csh shell, you can use a loop to perform a set of commands multiple times. Here are a few examples of loops in csh:

  1. For loop:

#!/bin/csh

set i = 1

while ( $i <= 10 )

 echo "Iteration $i"

 @ i++

end

This script will print "Iteration 1" to "Iteration 10".

  1. For loop with a range:

#!/bin/csh

foreach i ( 1 2 3 4 5 6 7 8 9 10 )

 echo "Iteration $i"

end

This script will print "Iteration 1" to "Iteration 10".

  1. For loop with command substitution:

#!/bin/csh

foreach i ( `seq 1 10` )

 echo "Iteration $i"

end

This script will print "Iteration 1" to "Iteration 10".