A loop is a way to do many things at once — or to make many mistakes at once if it does the wrong thing. One way to check what a loop would do is to echo the commands it would run instead of actually running them.
Suppose we want to preview the commands the following loop will execute without actually running those commands:
$ for datafile in *.pdb
> do
> cat $datafile >> all.pdb
> done
What is the difference between the two loops below, and which one would we want to run?
# Version 1
$ for datafile in *.pdb
> do
> echo cat $datafile >> all.pdb
> done
# Version 2
$ for datafile in *.pdb
> do
> echo "cat $datafile >> all.pdb"
> done
all.pdb
, as the >>
is treated literally as part of a string rather than as a redirection instruction.
echo cat $datafile
to the file, all.pdb
. This file will just contain the list; cat cubane.pdb
, cat ethane.pdb
, cat methane
.pdb etc.
all.pdb
file to view its contents.