According to bash manual:
The command substitution
$(cat file)
can be replaced by the equivalent but faster$(< file)
.
but I fail to see why. Let's try to take a closer look at what $(< file)
means and what it does:
- by definition, a
$(command)
construct expands to whatever command writes to stdout. - in our case, command is
< file
, so it consists solely of a stdin redirection to file. Does it write anything to stdout? My guess would be it doesn't (why would a stdin redirection alone affect stdout?), but apparently it does since$(command)
expands to contents of file instead of nothing.
Basically, I fail to see the "obvious" equivalence of $(cat file)
and $(< file)
. How does it follow from bash syntax? Is there a special rule explaining why stdin is copied to stdout in this particular case (or, generally, what happens to streams for empty commands, i.e. commands consisting solely of one or more redirections)? Is it a "bashism" or does the same equivalence hold true for other shells (especially dash) as well?