WezM.net Menu

Expand glob in variable within shell script

Published on Thu, 24 April 2008

I had the need today to read in a list of patterns from a file, expand them and then operate upon each file all from within a shell script. Apparently there is a shell built-in for csh, but I didn’t want to learn csh at that point. The eventual solution was to run an extra shell with the -c argument.

Example:

#!/bin/bash
FILES=`cat patterns.txt`
for pattern in $FILES; do
    bash -c "ls -l ${pattern}"
done

Something to Add?

Contact me by email or Twitter if you have a comment, correction or just want to get in touch.