[clue-tech] Bash script question

David L. Anselmi anselmi at anselmi.us
Wed Sep 14 18:28:21 MDT 2005


Jim Ockers wrote:
[...]
>>  #!/bin/bash
>>  # myscript
>>  while read file ; do
>>     echo got $file
>>  done
>>
>>
>>use as:
>>
>>  ls |  myscript
> 
> Will this work if the files have spaces in the names?  I didn't try
> it of course, just wondering.

Sure it will work, depending what you mean by work.

read works line by line and assigns words to each name given to it.  So 
if you want input with spaces to be assigned correctly you have to quote 
it correctly.  In this case the whole line (spaces and all) will be 
assigned to $file since it is the only name.

The variable expansion happens as usual too, so you might have to quote 
$file on the echo line.  For echo it doesn't matter.  In other cases it 
might, and it isn't too hard to come up with constructs that are 
difficult or impossible to quote correctly.

Dave
_______________________________________________
CLUE-tech mailing list
CLUE-tech at cluedenver.org
http://cluedenver.org/mailman/listinfo/clue-tech



More information about the clue-tech mailing list