IMO you shouldn't use
or
Both will break if your search finds any filenames with spaces e.g.
Code:
$ ls *.jpg
978-1-234 5678-9.jpg 978123456789.jpg
$ pics=$(find . -maxdepth 1 -name '*.jpg' -printf "%f\n")
$ if [ $pics ] ; then echo 'Pics found!'; fi
-bash: [: 978-1-234: binary operator expected
$
$ for pic in $pics; do echo $pic; done
978123456789.jpg
978-1-234
5678-9.jpg
A more bullet proof way to do this kind of loop-over-find-results is along the lines of (fill in whatever you want to 'do' instead of echo)
Code:
$ while read -rd $'\0' pic; do echo "$pic"; done < <(find . -maxdepth 1 -name '*.jpg' -print0)
You don't really need to test if there are any matches but if you really must then it's better to (a) quote the variable (make it a habit - ALWAYS!) and (b) test to see if the string is non-empty
Code:
$ if [ -n "$pics" ] ; then echo 'Pics found!'; fi
Pics found!
$
Bookmarks