How do I loop through only directories in bash?
If you need to select more specific files than only directories use
findand pass it to
shopt -s dotglob find * -prune -type d | while IFS= read -r d; do echo "$d" done
shopt -u dotglobto exclude hidden directories (or
unsetopt dotglobin zsh).
IFS=to avoid splitting filenames containing one of the
$IFS, for example:
see AsymLabs answer below for more
In case you need to create an exit value from within the while loop, you can circumvent the extra subshell by this trick:
while IFS= read -r d; do if [ "$d" == "something" ]; then exit 1; fi done < <(find * -prune -type d)