4

What are the operators <<and < used for in Linux?

For example

cat << abc.txt
Anthon
  • 78,313
  • 42
  • 165
  • 222
NoobEditor
  • 185
  • 1
  • 2
  • 11
  • 1
    Related question: [Correct textual name for <<](http://unix.stackexchange.com/questions/2888/correct-textual-name-for) – Ivan Chau Feb 10 '14 at 09:53

1 Answers1

7

< is used to redirect input. Saying

command < file

executes command with file as input.

The << syntax is referred to as a here document. The string following << is a delimiter indicating the start and end of the here document.

$ cat abc.txt
cat: abc.txt: No such file or directory
$ cat << abc.txt
> Hello!
> Hey :)
> abc.txt
Hello!
Hey :)
$

<< doesn't indicate any sort of indirection.


You might also want to refer to redirection and here document.

devnull
  • 10,541
  • 2
  • 40
  • 50
  • I personnaly still don't understand – Kiwy Feb 10 '14 at 08:54
  • @Kiwy What is it that you don't understand? – devnull Feb 10 '14 at 08:55
  • Well reading it third time I start to understand you should express it as a end of input delimiter instead it would be way more clear than writing some script and explaining after. – Kiwy Feb 10 '14 at 08:57
  • @devnull : so, `<<` will act as input if i say grep `"somestring" << filename.txt` ...just asking to clear out my head on this!! – NoobEditor Feb 10 '14 at 08:58
  • @NoobEditor In your example, `filename.txt` would be treated as a __delimiter__. Nothing would be read from the file. – devnull Feb 10 '14 at 09:00
  • @Kiwy I rephrased the answer. Hope it's more clear now. – devnull Feb 10 '14 at 09:00
  • I see that. that's better ;-) – Kiwy Feb 10 '14 at 09:01
  • @devnull : ok..its pretty clear, upvoted...and accepted ( *but if some1 else comes up with better answer then u have competition* )...can u give me some reference urls where i can see both of these commands in work???? – NoobEditor Feb 10 '14 at 09:06
  • 1
    @NoobEditor You might want to refer to a couple of links added in the answer. – devnull Feb 10 '14 at 09:08