Aug 212010
At work the other day, I found myself needing to install a bunch of gems with differing versions. I had a file created that looked something like this which had all the gems (along with specific versions) that were requested to be installed:
foo --version 1 bar --version 2 foobar --version 3
So, I tried running a bash for loop over the items to get them installed. However, I soon found that this wasn’t going to work:
$ for gem in `cat gems`; do echo $gem; done foo --version 1 bar --version 2 foobar --version 3
Bash was using any whitespace separator as indication of a new item in the loop. After searching around for a bit, I found that you can use the POSIX read utility to make this work as expected.
$ cat gems | while read line; do echo $line; done foo --version 1 bar --version 2 foobar --version 3
Exactly what I needed.