Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

bash does not seem to complain if you run a command like

$ < some-file-that-exists

Nothing seems to happen.

You could also use

$ <<<"any string"

and even process substitution.

So what exactly does this do, and why is it allowed. Could it be useful for anything?

share|improve this question
BTW: For the great ZSH, "< foobar" is the same as "less foobar", as your redirect foobar to the shell. I think this is a quite intuitive behavior. A bare < will give you an error (zsh: parse error near `\n') as no argument is given. – math Apr 11 '12 at 13:36
@math you will get a similar error in bash, but that is interesting about less. – Explosion Pills Apr 11 '12 at 13:53
up vote 19 down vote accepted

< sets up redirection for a command that can occur before or after the redirection instructions. I.e.

$ < file wc

works as well as

$ wc < file

But if you don't give a command, bash sets up the redirection and doesn't do anything else. For input redirection, this amounts to what you saw, nothing, if the file exists and is readable. If the file does not exist or is not readable bash will signal an error. For output redirection, the output file will be created if it does not exist. Any existing output file will be truncated if > is used; no truncation if >> is used.

share|improve this answer
in other words they are a concise exists and create – ratchet freak Apr 5 '12 at 23:12
@Kyle Jones I think you mismatched the two operators in the last sentence. – Puddingfox Apr 6 '12 at 1:20
@Puddingfox Thanks. I've edited the run-on sentence at the end to clarify. – Kyle Jones Apr 6 '12 at 1:52
Is there any reason to use < file wc vs. wc < file? – Explosion Pills Apr 6 '12 at 17:37
@tandu It comes in handy when using interactive shells. Say you have a command that uses redirection and you want to repeat it with a different last argument. If the redirection is at the beginning of the line, it's easy to type control-P and then edit the last argument without having to skip over the "< foo > bar 2>&1" language as you would if it were at the end. Beyond that, I've found no use for it. – Kyle Jones Apr 6 '12 at 19:31

< reads from stdin by default, or a file. your expression does not do anything with the input because you did not do anyting after reading.

if you do < some-file-that-exists > outputfile at the end, then you will see that you have just read in the content of the first file and wrote it to the second file.

<<< is here doc format, so it reads from a string instead from a file. cat <<<"any string" to see what you have read.

share|improve this answer
for future reference, please edit your answer to add detail instead of adding a comment – Nate Koppenhaver Apr 5 '12 at 20:17
fixed. thanks for your suggestion, nate! – johnshen64 Apr 5 '12 at 20:19
"if you do < some-file-that-exists > outputfile at the end then you will see that you have just read in the content of the first file and wrote it to the second file." This is wrong; without a command the output file will be truncated to zero bytes. – Kyle Jones Apr 5 '12 at 20:51

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .