Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does "cat > somefilename <<EOF" (particularly, the greater-than and double less-than symbols) do in shell?

Tags:

bash

shell

Just came across the following command:

cat > myspider.py <<EOF

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

like image 638
Jianxin Gao Avatar asked Sep 19 '16 22:09

Jianxin Gao


People also ask

What is cat << end?

This operator stands for the end of the file. This means that wherever a compiler or an interpreter encounters this operator, it will receive an indication that the file it was reading has ended.

What is EOM in shell script?

End Of Message (-EOM)

What is EOT in bash?

EOT is an ASCII character that historically signalled the end of a message (and is a special character in UNIX terminals that means end of stream when it appears in user input only), but it CAN appear in files, so using it in C to signal the end of a file would be a terrible idea when reading binary files!


1 Answers

<<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:

  • The bash-hackers redirection tutorial
  • The Wooledge wiki entry on Heredocs
like image 199
Charles Duffy Avatar answered Sep 28 '22 06:09

Charles Duffy