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?
set -x(orbash -x yourscript) and compared output between your script in that mode and running the command you want by hand, the issue would have been obvious.echo "${foo[@]}", by the way, you might consider getting in the habit ofprintf '%q ' "${foo[@]}"; echo.echo "${foo[@]}"is pretty much useless, becauseechodoesn't show you the difference betweenecho "hello world"andecho "hello" "world", so it gives you no idea whatsoever as to whether or not you have quoting problems in your code.