Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

In a bash script I cannot post here I see the following expression:

$(<"dir/file")

From what I understand the expression $(...) evaluates a command inside (as when using backticks), but what is the role of the angle bracket used inside the round bracket?

share|improve this question
3  
Command substitution allows the output of a command to replace the command itself. The command substitution $(cat file) can be replaced by the equivalent but faster $(< file): gnu.org/software/bash/manual/html_node/… –  skwllsp Nov 28 '13 at 9:36
2  
@skwllsp You can turn that into an answer. –  Teresa e Junior Nov 28 '13 at 11:25

1 Answer 1

up vote 3 down vote accepted

A redirection <"dir/file" opens the file for reading on standard input for the duration of the command that the redirection applies to. When there is no command, the file is open for reading (which leads to an error if the file doesn't exist or lacks proper permission), but other than that the redirection has no effect.

Ksh added an extension, which has been adopted by bash and ksh. If a command substitution contains an input redirection and nothing else $(<"dir/file") (no command, no other redirection, no assignment, etc.), then the command susbtitution is replaced by the content of the file. Thus $(<"dir/file") is equivalent to $(cat "dir/file") (except that it doesn't call the cat utility, so it's marginally faster and does the same thing even if cat has been replaced or isn't in the command search path).

This is mentioned in the bash manual under “command substitution”.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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