I've been experimenting with Bash and I noticed that when using here strings, filename expansion doesn't occur as I expected. Normally, filename expansion works with commands like `echo`, as shown in this example: `echo /var/cache/*` results in `/var/cache/apk /var/cache/misc`. However, when I try to use it in a here string like this: `cat <<< /var/cache/*`, it simply returns `/var/cache/*`. It's stated in the Bash documentation that filename expansion and word splitting are not performed in here strings. Is there a way to make filename expansion work in this context? Are there shell flags, patches, or workarounds? And why does variable expansion work but not filename expansion? I'd appreciate any insights or solutions!
6 Answers
I think it’s worth considering the context of filename expansions. They are not suited for here strings because you can't predict how filenames could behave, especially if they contain spaces or special characters. Using arrays to handle filenames gives you more control over how they are processed, allowing for cleaner splitting and quoting. Before arriving at a here string, create your array and manage each filename carefully.
Hey, it would be great if you could share what output you're expecting from your commands! What’s your ultimate goal here? This might help people give you tailored answers based on what you’re trying to achieve!
Instead of trying to get filename expansion directly in a here string, you might want to consider using an array. You can expand your filenames into an array first and then use that where needed. This way you’ll avoid the pitfalls that come with handling multiple filenames in a context that doesn’t support them. Keep in mind, filename expansion in here strings doesn't happen because they can produce multiple words from one single command, and the shell isn't set up to handle that right there. It's all about managing that context properly!
From what I can see, the behavior you're experiencing is indeed what's expected! The here string syntax is designed not to perform filename expansion. What you can do is perform the expansion in a variable first. For instance, you can use `set -- *; cat <<< $*` to achieve this indirectly. Although there could be better reasons than feeding that string as input. Just remember that here strings treat the content as standard input, not command arguments, which is why filename expansion doesn't activate.
You might want to check out `$(realpath filename)` as a way to handle path resolution while still keeping things straightforward and manageable. It could simplify matching paths that you’re particularly interested in. Just a thought!
The main reason filename expansion isn’t performed in here strings is how these strings are processed—instead of being treated as arguments to a command, they’re sent as standard input. You can work around this by using command substitution to capture the expanded filenames first, like this: `cat <<< "$(echo /var/cache/*)"`. That way, the `echo` will expand the filenames and pass them correctly to `cat`. Another option is to use a pipe: `echo /var/cache/* | cat`. This also achieves what you want without relying on here strings. Hope that helps!

Related Questions
How To: Running Codex CLI on Windows with Azure OpenAI
Set Wordpress Featured Image Using Javascript
How To Fix PHP Random Being The Same
Why no WebP Support with Wordpress
Replace Wordpress Cron With Linux Cron
Customize Yoast Canonical URL Programmatically