Reputation: 1130
I'm trying to build up an array and then use it as the arguments to a script. When I echo out the command I think I'm building, it looks fine. But when I try to execute it, nothing happens. Here's a small example. In my actual script, I have to build the array at run time so I can't hard code (/tmp -iname "*.log*")
And it has to run in older bash environments too, so I can't use +=
to append to an array.
#!/bin/bash
args=( /tmp )
args[${#args[@]}]=-iname
args[${#args[@]}]="\"*.log\""
# the following echoes what I expect: find /tmp -iname "*.log"
echo find "${args[@]}"
# this next line does not appear to find any files
find "${args[@]}"
echo the following finds files
find /tmp -iname "*.log"
What am I doing wrong?
Upvotes: 1
Views: 33
Reputation: 784958
Don't use quotes inside quotes, this should work:
#!/bin/bash
args=( /tmp )
args[${#args[@]}]=-iname
args[${#args[@]}]='*.log'
find "${args[@]}"
Upvotes: 3