9

Just came across the following command:

cat > myspider.py <<EOF

But I'm not sure of the use of > and <<.

Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
Jianxin Gao
  • 2,717
  • 2
  • 19
  • 32
  • Arguably this should be two completely different questions. (Also, they're almost certainly both duplicates). – Charles Duffy Sep 19 '16 at 22:37
  • Hmm. http://stackoverflow.com/questions/39581182/what-does-this-bash-line-do-hpc is a perfect duplicate, but has no answers (and a downvote). Closing that as a dupe of this one. – Charles Duffy Sep 19 '16 at 22:43
  • BTW, the original title was less clear because `>` and `<<` can mean different things if they were, for instance, used in a math context. – Charles Duffy Sep 19 '16 at 22:47
  • 1
    For the manual page (succinct, to the point of being terse), see [I/O Redirection](https://www.gnu.org/software/bash/manual/bash.html#Redirections). – Jonathan Leffler Sep 19 '16 at 22:47
  • @CharlesDuffy My original title with "greater than" and "double less than" literals were intended to make the question more searchable. Or I'm wrong and the actual symbols will work just fine. – Jianxin Gao Sep 19 '16 at 22:59
  • 1
    See also the SO documentation page for [bash redirection](http://stackoverflow.com/documentation/bash/399/redirection#t=201609192341380811663) – Eric Renouf Sep 19 '16 at 23:42

1 Answers1

9

<<EOF is the start of a heredoc. Content after this line and prior to the next line containing only EOF is fed on stdin to the process cat.

> myspider.py is a stdout redirection. myspider.py will be truncated if it already exists (and is a regular file), and output of cat will be written into it.

Since cat with no command-line arguments (which is the case here because the redirections are interpreted as directives to the shell on how to set up the process, not passed to cat as arguments) reads from its input and writes to its output, the <<EOF indicates that following lines should be written into the process as input, and the >myspider.py indicates that output should be written to myspider.py, this thus writes everything up to the next EOF into myspider.py.


See:

Charles Duffy
  • 280,126
  • 43
  • 390
  • 441