LINUX CLASSES - PROGRAMMING
How Does Shell Script Looping Work?
Shell scripts written in Bash can implement looping , or iteration, with the while , until ,
and for constructs. In each case, a block of code is executed repeatedly until a loop exit
condition is satisfied. The script then continues on from that point.
The while Statement
In a while loop, the block of code between the do and done statements is executed so
long as the conditional expression is true. Think of it as saying, "Execute while this
condition remains true." Here's an example:
while [ "$*" != "" ]
echo "Argument value is: $1"
This trivial example prints the value of each argument passed to the shell script.
Translated to English, the while condition says to continue so long as the input argument
string is not null. You could also code the while statement as
while [ -n "$*"
but I think the first method is much easier to read and understand.
You might think that this loop would continue to print the first argument ($1) forever and
ever, since you don't expect the value of the $* variable (the list of arguments from the
command line) to change during the course of running the script. You'd be right, except
that I slipped the shift command into the body of the loop.
What shift does is discard the first argument and reassign all the $n variables--so the
new $1 variable gets the value that used to be in $2, and so on. Accordingly, the value in
$* gets shorter and shorter each time through the loop, and when it finally becomes null,
the loop is done.
The until Statement
The until construct works almost exactly the same as while. The only difference is that
until executes the body of the loop so long as the conditional expression is false, whereas
while executes the body of the loop so long as the conditional expression is true. Think
of it as saying, "Execute until this condition becomes true."
Let's code the previous example using an until loop this time and making it a little
fancier by adding a counter variable:
until [ "$*" = "" ]
echo "Argument number $count : $1 "
count=`expr $count + 1`
Again, you could have coded the until statement as
until [ -z "$*"
but I recommend not using the -n and -z operators because it's harder to remember what
The only new concept here is the strange-looking line that increments the counter:
count=`expr $count + 1`
The expr command signals to the shell that we're about to perform a mathematical
calculation instead of a string operation. And the doodads that look kind of like single
quotation marks are not--they're the backtick (`) character, found to the left of the
number 1 key on most keyboards. By enclosing an expression in backticks, you tell the
shell to assign the result of a Linux command to a variable, instead of printing it to the
Note: The spaces on either side of the plus sign are required.
The for Statement
The for statement is yet another way to implement a loop in a shell script. The general
form of the for construct is shown here:
for item in list
something useful with $item
Each time through the loop, the value of the item variable is assigned to the nth item in
the list. When you've processed all the items in the list, the loop is done. Here's an
example similar to the until and while loops you saw earlier in this section:
for item in "$@"
echo "Argument value is: $item"
Note that in this example, I used the special variable $@ instead of $* as the list of arguments.
The variables $@ and $* both contain the command line arguments, but
the for command expects an array (a list of items) as input.
Previous Lesson: Shell Script Logic
Next Lesson: Shell Script Debugging
[ RETURN TO INDEX ]
Comments - most recent first
(Please feel free to answer questions posted by others!)
(20 Sep 2012, 04:30
I am new to Shell Scripting and way behind in my class. Unfortunately, I am
in a accelerated 10 wk class and my instructor is working the previous
instructors lessons plans. I feel so lost and discouraged. If anyone is
available show me how to write a shell script using the below parameters
that would be great.
A glass return company requires a program which will calculate the amount
of credit due to a customer who returns cases of empty bottles. (One case
contains 10 bottles.)
Input to the program is a record containing the customer's name, the number
of full cases and the number of bottles in a partial case, if any. If a
partial case returned by a customer contains 5 or more bottles, it is to be
counted as a full case. Partial cases of less than 5 bottles are not
If 8 or more cases are returned, the customer is credited $4.00 per case,
otherwise the customer is credited $3.00 per case.
The program is to print the customer's name, the number of full cases
credited, and the credit amount due.
Number of cases
Number of bottles in last case
If number of bottles is grater then or equal to 5, then add 1 to cases
If Total Cases grater then or equal to 8, then multiply cases by 4
else then multiply cases by 3
Total Cases credited
(02 Jul 2011, 09:37
Great tutorial for beginners!!
(14 Mar 2011, 05:53
find ./WEB-INF -type f |cat > result.txt
by using this i am geting output like :
now i need to take each line of this output process tht line and remove
that line from file after that next line ,
logic can be we count line number such as n=3
use for or while loop on it
tail -1 --it gives last line
take tht line process it
remove tht line from file
this is wht i think but not able to find correct syntax for it in shell
script...could anyone help me its very urgent for me...provide me correct
Thanks in Advance...
(08 Mar 2011, 13:47
Hey people! I was just seeking for some info on this topic. Do you know
more good forums or other similar resources about this?
(04 Mar 2011, 09:28
I like this forum. Iām happy to have found so much useful info here.
Thanks to you all people!
(19 Oct 2010, 09:43
In the for loop example the first line should be:
for item in "$@"
(18 Oct 2010, 01:18
give me notes of unix shell programming please
(13 May 2010, 07:22
Very nice and clear examples for beginners, but for loop explanation is
shallow and not quite clear for beginners
(10 May 2010, 14:55
@Dennis: I just realized this might also be useful for you.
while read LINE
done < yourfile
(10 May 2010, 14:54
@Dennis: I use this construction all the time.
for ITEM in `cat yourfile`
Note that each item is separated by any whitespace, so multiple items on a
line will be treated separately.
(05 May 2010, 03:57
how to imput arguments in loop from file?
(15 Apr 2010, 00:14
Very nice examples for beginners
(12 Feb 2010, 12:39
Good catch! Fixed now...
(12 Feb 2010, 12:26
Nice set of examples. Minor note, in the last for loop you have:
for $item in "$*"
it should read:
for item in "$*"
I welcome your comments. However... I am puzzled by many people
who say "Please send me the Linux tutorial." This website *is* your Linux Tutorial! Read everything here, learn
all you can, ask questions if you like. But don't ask me to send what you already have. :-)
NO SPAM! If you post garbage, it will be deleted, and you will be banned.