Skip to Content

while

Bash Loops

While

Counting to 3 and doing something along the way right on the command line.


# i=1; while [ "$i" -lt 4 ]; do echo $i; i=$(($i+1)); done
1
2
3

The same command in a script:

#!/bin/bash
i=0
while [ "$i" -lt 10 ]
do
   echo $i
   i=$(($i+1))
done

For

Iterate through lines in a file with a for loop on the command line.


# for a in $(cat /tmp/users); do echo $a; done
usera
userb

In a script:

#!/bin/bash
for a in $(cat /tmp/users)
do

Syndicate content