I am trying to understand how exactly Bash treats the following line:$(< $FILE)According to the Bash man page, this is equivalent to:$(cat $FILE)and I can follow the line of reasoning for this second line. Bash performs variable expansion on $FILE, enters command substitution, passes the value of $FILE to cat, cat outputs the contents of $FILE to standard output, command substitution finishes by replacing the entire line with the standard output resulting from the command inside, and Bash attempts to execute it like a simple command.However, for the first line I mentioned above, I understand it as: Bash performs variable substitution on $FILE, Bash opens $FILE for reading on standard input, somehow standard input is copied to standard output, command substitution finishes, and Bash attempts to execute the resulting standard output.Can someone please explain to me how the contents of $FILE goes from stdin to stdout?
Understanding Bash's Command Substitution
linux;bash;shell
The < isn't directly an aspect of bash command substitution.  It is a redirection operator (like a pipe), which some shells allow without a command (POSIX does not specify this behavior).Perhaps it would be more clear with more spaces:echo $( < $FILE )this is effectively* the same as the more POSIX-safeecho $( cat $FILE )... which is also effectively*echo $( cat < $FILE )Let's start with that last version.  This runs cat with no arguments, which means it will read from standard input.  $FILE is redirected into standard input due to the <, so cat puts its contents are put into standard output.  The $(command) subsitution then pushes cat's output into arguments for echo.In bash (but not in the POSIX standard), you can use < without a command.  bash (and zsh and ksh but not dash) will interpret that as if cat <, though without invoking a new subprocess.  As this is native to the shell, it is faster than literally running the external command cat.  *This is why I say effectively the same as.