Looping on the Command Line


We will loosely follow the Software Carpentry Loops lesson from The Unix Shell


That lesson does not cover if-tests, so we will supplement that here. Some information can be found on the Linux Documentation Project Conditional Statements section.

And if-test has the form:

if [ condition ]
   # do something

where condition is evaluated, and if true, we take the action specified in the block. fi is the end-if statement, ending the block of code that is conditionally executed.

Let’s go back to looping over the files in creatures/ that the Software Carpentry lesson did. But let’s add some that don’t exist and have our loop test for that

for creature in basilisk.dat pegasus.dat minotaur.dat centaur.dat unicorn.dat
   if [ ! -e ${creature} ]
      echo creature ${creature} does not exist

Now, let’s look back at the loop we put into our mybashrc and now we can understand what it is doing:

for dotfile in ~/MySBFiles/dotfiles/.*
   if [ ! -f ~/`basename $dotfile` ]; then
      ln -s $dotfile ~/;

Notice that you can put the then on the same line as if if you separate with a ;. There is still one new command here, basename and one new piece of syntax – using the backticks, `.

We can see what basename does by looking at the man page:

man basename

The backtick is a special syntax in a shell command. Essentially it means:

  • first execute any commands inside the openning ` and closing `

  • substitute the output of the command in place and then use do the rest of the script

Note that an equivalent way of doing this in Bash is to use $( ... ).

So for example, we could do:

for creature in `ls *.dat`
   echo ${creature}

(but for this simple example, we also know that a wildcard would work here).

Here’s an example where we change the name of the extension of the file from .dat to .out

for creature in *.dat
   echo `basename $creature .dat`.out


When we do ${creature} above, we are treating asking Bash to substitute the value of the variable creature. There are actually a lot of handy operations we can do on shell variables, as you can see here: http://www.gnu.org/software/bash/manual/html_node/Shell-Parameter-Expansion.html


We learned the following commands:

  • for, do, and done : the syntax for a loop

  • if and fi : the syntax for an if-test

  • history : see the list of commands we used in this session