sampleDir=/home/sample*
assigns a literal '/home/sample*'
into sampleDir
$ echo "$sampleDir"
/home/sample*
but if you don't double-quote the variable expansion, it will undergo glob expansion (unless set -f
is on)
and splitting on $IFS
characters (characters in the IFS
variable— normally space, tab, and newline).
You can count the number of items after the expansion by passing the unquoted $sampleDir
to a function that
counts its arguments.
argc() { argc=$#; }
argc $sampleDir
echo $argc #will print the number of items $sampleDir expanded to
This would be how you can do it portably, on any POSIX shell (solutions based on things like arrays are limited to shells that have them).
( I recommend returning stuff from shell functions by assigning it to a global variable of the same name as the function.
It's portable, namespace-clean, and very fast (unlike echoing and then using captures to get the echoed string, which is common, but rather expensive))