for line in $( if [ $recursive = "1" ]; then find $targetdir -mindepth 1 -print else find $targetdir -mindepth 1 -maxdepth 1 -print fi ); do # ...whatever done ####### if [ $recursive -eq 1 ]; then CMD="find $targetdir -mindepth 1 -print" else CMD="find $targetdir -mindepth 1 -maxdepth 1 -print" fi for line in $( $CMD ); do #WHATEVER done