Nested double quotes won't work in my bash script?

In a bash script I have:

LSCMD="find /project/media/ -mindepth 2 -maxdepth 2 -name \"files*pkg\""
ALL_PACKAGES=$( $LSCMD | sort 2>/dev/null)

But I get nothing returned. It's just all blank. If I run the find command in a terminal, I get dozens of hits.

I figure it's the way how I'm escaping the double quotes, but after several google articles, I'm pretty sure it's allowed in bash. I even tried single quotes, 'files*pkg', but that didn't work either.

Any help?

Thanks!

What happens when you use set -x option?

Just don't use the escaped double quotes at all. Try without them and it will fly.

You cannot quote quotes and expect the shell to unquote them for you. When you execute $LSCMD, it takes those quotes literally. Leave them out...

You might also be in for another surprise -- $LSCMD will still substitute wildcards -- everywhere, because quotes have no meaning inside it. So if you have a file named 'files2pkg' in the current directory, it can shove that in instead of files*pkg. You can turn this off with set -f, so try this:

LSCMD="find /project/media/ -mindepth 2 -maxdepth 2 -name files*pkg"

set -f # Prevent * from expanding when $LSCMD splits.
ALL_PACKAGES=$( $LSCMD | sort 2>/dev/null)
set +f # Turn * expansion back on

Another thing you could do is use a function instead of storing things in a string. You wouldn't need to do any special quoting of anything, it'd just be code as usual.

lscmd() {
        find /project/media -mindepth 2 -maxdepth 2 -name "files*pkg"
}

ALL_PACKAGES=$( lscmd | sort 2>/dev/null )