Jianxin Gao
Jianxin Gao

Reputation: 2777

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

Just came across the following command:

cat > myspider.py <<EOF

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

Upvotes: 10

Views: 7802

Answers (2)

piyush parmar
piyush parmar

Reputation: 1

"<<" is a symbol of input redirection in shell script. In order to print multiple line of string's input in a console or want to put those strings into a file, we can use '<<' symbol.

let's understand this by the example:

script 1:
cat <<EOF
>HI 
>HOW ARE YOU?
>EOF

output:
HI
HOW ARE YOU

script 2:
cat <<EOM
>HI
>HOW ARE YOU
>EOM

output:
HI
HOW ARE YOU

script 3:
cat <<MYWORD
>HI
>HOW ARE YOU
>MYWORD

output:
HI
HOW ARE YOU

Here EOF(End Of File) and EOM(End Of Message) is used to put multiline input in the console. However you can use any keyword instead of EOM or EOF by your choice like we have used MYWORD here. If you want to put those strings in files:

cat > file.txt <<MYWORD
>HI
>HOW ARE YOU
>MYWORD

Upvotes: 0

Charles Duffy
Charles Duffy

Reputation: 295443

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

Upvotes: 11

Related Questions