How do I get a list of directories in bash and then expand them as command line parameters?

Viewed 22350

I'm writing a bash script which needs to, for one step, get a list of directories (variable) in a target directory (which may also contain files), and then expand them out as parameters to a python script.

Example:

/stuff/a dir/
/stuff/b other/
/stuff/c/

And I need to, within a bash script, call:

script.py "a dir/" "b other/" "c/"

or alternately, escaped spaces:

script.py a\ dir/ b\ other/ c/

I need the script to be called exactly once for directory 'stuff'.

Is there a straightforward way to do this kind of thing? I've been googling around and the best I've managed to figure out requires me to know how many directories there are.

4 Answers

A simpler solution that does not create a new process (as find does) is:

for f in stuff/*; do
  if [ -d "$f" ]; then
     ./script.py "$f"
  fi
done
Related