-2

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?

Thanks.

Tim
  • 101,790

1 Answers1

3

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)
Alex
  • 1,175
  • 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
  • No, that's not correct. $(< is a special operator. It's not the same as running < file in a command substitution which would expand to nothing. – Stéphane Chazelas Aug 23 '17 at 15:15
  • Hmm, I'm not so sure about that. I just ran echo \< file``, which is the other form of command substitution, and it seems to have executed just fine... – Alex Aug 23 '17 at 15:21
  • Yes both \<and$(<. If it was normal command-substitution, you'd need no output at all, like when you do< b.sh` (in shells other than zsh). See my answer to the duplicate question for details. – Stéphane Chazelas Aug 23 '17 at 15:23
  • @StéphaneChazelas Oh, wow, you should post something to have your answer as the accepted one on this post too. – Alex Aug 23 '17 at 15:31
  • Well, this one is now closed as duplicate. You could correct yours here and add a link to mine there for further details. – Stéphane Chazelas Aug 23 '17 at 16:05