From Bash Manual:

The command substitution $(cat file) can be replaced by the equivalent but faster $(< file).

cat is from coreutils, instead of a bash builtin command. So I am surprised that the bash manual would mention something outside bash, such as cat.

Is $(< file) really a shorthand of $(cat file)?

Is $(< file) completely bash, or does $(< file) really depend on cat?

If $(< file) is completely bash, and doesn't depend on cat,

  • is < file a redirection or a command or both (a command with a redirection and an empty command name)?
  • how can < file (a command with a redirection and an empty command name) output to stdout?



Update: see https://unix.stackexchange.com/a/368663/145784 for a full discussion of unix operators, such as $(<, in different shells.

$(< file) is completely bash. The statement < file takes the file, and redirects it to stdin of the command it is invoked with. In this case, $() is a form of command substitution, so it is used as a substitute for a command. It takes the input it receives, and converts it into a variable suitable to be used in a command. Since it does not invoke an outside program cat, it is therefore faster.

To output this variable to stdout, you would simply run:

echo $(< file)
|improve this answer|||||
  • Thanks. is < file a redirection or a command or both (a command with a redirection and an empty command name)? how can < file output to stdout without a command name? We need a command name which performs the output operation, right? – Tim Aug 23 '17 at 14:52
  • Give me a moment. I just submit the post before finishing it, to have something out there while I continue answering the question. Let me know whether this answers your question now. – Alex Aug 23 '17 at 14:55
  • I mention output to stdout, not because I want to know how to output to stdout (which you answered by echo $(< file)) . It is because command substitution $(some-command) assumes the command inside $(...) outputs to stdout, so that command substitution captures its stdout output and then expands to the captured. If a command doesn't output to stdout, how can command substitution work with the command? – Tim Aug 23 '17 at 14:56
  • Thanks for the clarification. I updated the post to address this question (which I didn't glean from the original post, sorry). Let me know if this makes sense now. – Alex Aug 23 '17 at 15:01
  • Thanks. A redirection like < file can apply only to a command. $() isn't a command, and even if it were a command, shouldn't < file follow $() instead of appearing inside? – Tim Aug 23 '17 at 15:04

Not the answer you're looking for? Browse other questions tagged or ask your own question.