Quote the variable directory portion while leaving the intended wildcard outside quotes. Capture matches in an array when you need to preserve filenames as separate arguments.
Reproduce the behavior safely
Run this self-contained fixture in an empty temporary directory, separate from your project. Shell examples create only named fixture files there. JavaScript examples use local values and send no network requests.
mkdir 'sample files'
touch 'sample files/a.txt' 'sample files/b two.txt'
dir='sample files'
literal=("$dir/*.txt")
matches=("$dir"/*.txt)
test "${#literal[@]}" -eq 1
test "${literal[0]}" = 'sample files/*.txt'
test "${#matches[@]}" -eq 2
printf 'match: <%s>\n' "${matches[@]}"Observed local output
match: <sample files/a.txt>
match: <sample files/b two.txt>Why the result changes
The quotes around the entire first word protect the asterisk too, so it remains text. In the second word, quoting the directory preserves its space while the unquoted suffix remains a filename pattern. The array stores two paths, including the path containing a space, without converting the result to a line-based string.
Choose and verify the correction
Keep trusted pattern syntax in the command and data directories in quoted expansions. Pass the final array with "${matches[@]}" so each path remains one argument. Decide separately what should happen when there are no matches; Bash default behavior retains an unmatched pattern, and nullglob or failglob changes that policy. Use the related no-match guide when the directory can be empty. Avoid eval and parsing ls output to force expansion.
Limits and next checks
The fixture creates only an isolated directory and two ordinary files. Dotfiles need a separate explicit pattern or shell option, and options can affect sorting and matching. Brace expansion inside variable text does not become active just because a wildcard is active elsewhere. These results describe Bash arrays, not an invocation copied into POSIX sh or another shell.
Related: Bash Loop Runs Once With No Matching Files: Choose Nullglob or an Error.
Isolated fixture executed on macOS on November 10, 2026 (KST), with GNU bash, version 3.2.57(1)-release (arm64-apple-darwin26). The observed output verifies this example, not every application or runtime.