What does <<EOF do?

155,374

Solution 1

An excerpt from Shell Input/Output Redirections:

Here Document

A here document is used to redirect input into an interactive shell script or program. We can run an interactive program within a shell script without user action by supplying the required input for the interactive program, or interactive shell script.

  • The general form for a here document is:
    command << delimiter
    document
    delimiter
    
    Here the shell interprets the << operator as an instruction to read input until it finds a line containing the specified delimiter. All the input lines up to the line containing the delimiter are then fed into the standard input of the command.

    The delimiter tells the shell that the here document has completed. Without it, the shell continues to read input forever. The delimiter must be a single word that does not contain spaces or tabs.

  • Following is the input to the command wc -l to count total number of line:
    $wc -l << EOF
    This is a simple lookup program for good (and bad) restaurants in Cape Town.
    EOF
    3
    

  • You can use here document to print multiple lines using your script:
    #!/bin/sh
    
    cat << EOF
    This is a simple lookup program for good (and bad) restaurants in Cape Town.
    EOF
    
    This would produce:
    This is a simple lookup program for good (and bad) restaurants in Cape Town.
    

  • This runs a session with the vi text editor and saves the input in the file test.txt:
    #!/bin/sh
    
    filename=test.txt
    
    vi $filename <<EndOfCommands
    i
    This file was created automatically from a shell script
    ^[
    ZZ
    EndOfCommands
    

  • If you run this script with vim acting as vi, then you will likely see output like:
    $ sh test.sh
      Vim: Warning: Input is not from a terminal
    
    After running the script, you should see the following added to test.txt:
    $ cat test.txt
      This file was created automatically from a shell script
    

Solution 2

CAT< New.txt (Press Enter) Here, the user will be prompted to type the input to the file "New.txt". Then Press the Cntrl+d command to tell that this is the End of the file.

Instead of using cntrl+d, Cat<<EOF command can be used in this scenario and type EOF at the end of the paragraph. The system will consider EOF as the End of the para. Instead of EOF, any alphabets can be used. eg) CAT<<ZZZ New.txt (Press Enter). Here, the User will be prompted to type the input. At the end of the para, type ZZZ, then press enter. The system will consider ZZZ as the End of the para and it will come out of it.

Share:
155,374

Related videos on Youtube

LED Fantom
Author by

LED Fantom

Updated on September 18, 2022

Comments

  • LED Fantom
    LED Fantom over 1 year

    I am a newbie in Linux admin and while I'm learning GDB to debug my code, I need to create an input.txt file for my program to read. I know redirection symbols such as >, >>, and <, but couldn't find info about << via Google since it ignores it.

    What does the <<EOF do below?

    cat >input.txt <<EOF
    
  • kingmilo
    kingmilo almost 5 years
    +1 for the Cape Town reference haha
  • Daniel Kaplan
    Daniel Kaplan over 2 years
    Your wc example returns 1 not 3, for me.