home | changes | index | login

Loops In Bash

BashTricks. I always forget how to do this, and I'm sick of looking it up for that once every two months when I need it.

Don't feel like you have to string this all together on one line when you're doing it in an interactive shell. Just hit enter after each line.

To steal an example:

for planet in "Mercury Venus Earth Mars Jupiter Saturn Uranus Neptune Pluto" do echo $planet done

Note that list can be the output of a given program - just use backticks, like so:

for doc in ls *.doc do file $doc done

A real world example:

for release in ls do iconv -f iso-8859-1 -t utf-8 ./$release ../converted/$release.html done

You probably should use globs instead, though:

for release in * do foo ./$release done

pick a name (required to comment or edit a page)