On 2/12/06, David Berg <[EMAIL PROTECTED]> wrote: > I'm trying to write a for loop that descends into a list ofdirectories and > runs a command. I can't seem to get the quoting rightthough. Most of the > directories have spaces and they are makingthings difficult for me. Here is > what I have: > for DIR in dir\ 1 dir\ 2 dir\ 3; do cd $DIR pwd cd ..done > The spaces in $DIR aren't escaped. > Any help, including a pointer to the relevant part of TFM, is much > appreciated. > --Dave
Putting quotes around the $DIR variable is the solution here, however there's another option which may come in handy: you can use "while" loops instead of "for" loops. The problem with this option is that if the programs within the loop take over stdin for any reason, the loop will terminate prematurely (ssh does this, for example). cat list.txt | while read DIR do cd "$DIR"; pwd; cd .. done (Btw: I'd recommend wrapping the cd with a if [ -d "$DIR" ]; then; fi set of directives because while the cd "$DIR" may not always succeed, cd .. always will.)