# # Bash looping basic
APLawrence.com -  Resources for Unix and Linux Systems, Bloggers and the self-employed

2005/06/02 Bash looping

I've removed advertising from most of this site and will eventually clean up the few pages where it remains.

While not terribly expensive to maintain, this does cost me something. If I don't get enough donations to cover that expense, I will be shutting the site down in early 2020.

If you found something useful today, please consider a small donation.



Some material is very old and may be incorrect today

© June 2005 Tony Lawrence

There are so many different ways to create bash loops. Let's just look at a few. After the first, I'll only list the initial line:



for i in 1 2 3 4 cow
do
 echo $i
done

# alternate:

for i in 1 2 3 4 cow; do echo $i;done

for i in `seq 1 10`

for ((j=1; j <= 10; j+=2))

while ((x++ < 10))

until ((x-- < -10))

for i in {1..10}  (as of Bash 3.0)

for i in $(ls)

for in in `cat /tmp/somefile`



Two other commands are useful within loops: "break" exits a loop, and "continue" goes to the next iteration:

for i in 1 2 3 4 cow 6 7 8
do
  # skips "cow"
  ((i * 1 ==  0)) && continue
 echo $i
done

for i in 1 2 3 4 cow 6 7 8
do
  # stops after "4"
  ((i * 1 ==  0)) && break
 echo $i
done
 

Arrays can be looped:

array=(one two three)
for x in ${array[@]}
do
   echo $x
done

array=(one two three)
for ((x=0; x < ${#array[@]} ; x++))
do
   echo ${array[$x]}
done
 

A problem that often comes up for new users is looping over certain files. For example, you might want to do something to every .txt file:

for i in *.txt
do
 something $i
done
 

That works, but if there are no ".txt" files, "something" gets passed "*.txt" just the same. If that's not a good thing, you need to do more:

if `ls *.txt > /dev/null 2>&1`
then
for i in *.txt
do
 something $i
done
fi
 

Or you might do something like this:

count=`ls *.txt 2>/dev/null | wc -l`
if [ "$count" -gt 0 ]
then
 echo "$count files to back up"
 for i in *.txt
 do
  something $i
 done
fi  
 

Yet another way to do something like that is:

set -- *.txt
[ -f "$1" ] && dosomething *.txt
 

If the set gets nothing, $1 will be empty. Another advantage is that you don't loop over each file if you don't need to.

As you've seen, there are a lot of different ways to do bash looping. Which you use depends mostly on on what you are trying to accomplish, and to a lesser extent on your own style and preferences.


If you found something useful today, please consider a small donation.



Got something to add? Send me email.





(OLDER)    <- More Stuff -> (NEWER)    (NEWEST)   

Printer Friendly Version

->
-> Bash looping basic

1 comment


Inexpensive and informative Apple related e-books:

Take Control of Automating Your Mac

Take Control of OS X Server

Photos: A Take Control Crash Course

Sierra: A Take Control Crash Course

iOS 10: A Take Control Crash Course





More Articles by © Tony Lawrence







Mon Aug 8 14:46:51 2011: 9679   TonyLawrence

gravatar


Now that Bash 3 is becoming more common (with Bash 4 lagging behind), you should also see Bash 3 brace expansion (link)




------------------------


Printer Friendly Version

Have you tried Searching this site?

This is a Unix/Linux resource website. It contains technical articles about Unix, Linux and general computing related subjects, opinion, news, help files, how-to's, tutorials and more.

Contact us


Printer Friendly Version





The history of the world teaches us that succession is dangerous and that the strong take what they want. It's not likely to be any different with Linux. (Tony Lawrence)




Linux posts

Troubleshooting posts


This post tagged:

Linux

Scripting

Shell



Unix/Linux Consultants

Skills Tests

Unix/Linux Book Reviews

My Unix/Linux Troubleshooting Book

This site runs on Linode