There are two problems here. The first problem is that you're defining the two variables as arrays with only one element, and then using them in the find
command as simple, scalar variables. bash obligingly returns the first element of the array for each when you do that.
The second, as C.Aknesil points out, is that bash does not remove double-quotes after expanding a variable. In truth, this is not actually a problem, the problem is that you didn't know it or why bash behaves like this - "it's a feature, not a bug".
Try it like this:
TYPE=(-type f)
NAME=(-name '*log*')
'*log*'
needs to be quoted here so that bash doesn't expand the glob and add all matching files in the current directory to the array.
Then, when you need to use them with find
:
find "$2" "${TYPE[@]}" -mmin "+$HOUR_TO_MIN"
or
find "$2" "${TYPE[@]}" "${NAME[@]}" -mmin "+$HOUR_TO_MIN"
BTW, note that both $2
and $HOUR_TO_MIN
should be double-quoted here, but curly braces {}
are not needed because there is no need to disambiguate them from surrounding characters. e.g. $2
does not need {}
, but ${2}3
would need them because it would be interpreted as $23
(and not as "$2 followed by a 3") by the shell without them.
$HOUR_TO_MIN
is probably OK to not quote (because it's probably defined in the script and probably doesn't have problem characters like spaces etc in it) but it doesn't hurt to quote it and "always quote your variables (except when you KNOW with absolutely certainty that you don't want to, and WHY)" is a good habit to get into.
$2
, however, is provided by the user as an argument to the script and could have anything in it, and should always be quoted.
Note: it's not a good idea to do something like LST_FILES=$(find ...)
because filenames can have all sorts of annoying characters in them, from whitespace like spaces, tabs, and newlines to shell meta-characters like ;
, &
, >
, and |
. The only character that can not be in a filename is a NUL, which means that NUL is the only reliable filename separator that will work with any filename.
Instead, if you need the output of find in a variable, use an array and tell find
to use NUL as the filename separator with -print0
. e.g.
mapfile -d '' LST_FILES < <(find "$2" "${TYPE[@]}" "${NAME[@]}" -mmin "+$HOUR_TO_MIN" -print0)
The mapfile
command (also known as readarray
) reads from stdin and uses it to populate an array. In this case, we're telling to use NUL as the delimiter with -d ''
.
Try the following as an experiment to see how it works for yourself:
$ mkdir junk
$ cd junk
$ touch foo bar baz 'file with spaces' $'file\nwith\nLFs'
$ ls
bar baz file?with?LFs file with spaces foo
$ mapfile -d '' LST_FILES < <(find . -type f -print0)
$ declare -p LST_FILES
declare -a LST_FILES=([0]="./foo" [1]=$'./file\nwith\nLFs' [2]="./baz"
[3]="./file with spaces" [4]="./bar")
$ echo "${#LST_FILES[@]}" # use ${#...} to count elements in an array
5
$ for f in "${LST_FILES[@]}"; do echo "$f" ; done
./foo
./file
with
LFs
./baz
./file with spaces
./bar