Unix EOF

Posted on 2010-08-17
Last Modified: 2012-05-10

I have something in my code as  
Can u please explain me the above code snippet?
Question by:r4ranjor
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 84

Accepted Solution

ozo earned 500 total points
ID: 33461649
LVL 11

Expert Comment

by:Pieter Jordaan
ID: 33461970

 << means that input follows
EOF tells it to continue reading until it gets another EOF.

You can change it to anything, but it has to be unique. If there is an EOF anywhere else in the text that follow, it will stop reading.

This will also work:

Expert Comment

ID: 33477955
A simple tutorial on UNIX "Here is" Command

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

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 counto total number of line:

[amrood]$wc -l << EOF
      This is a simple lookup program
      for good (and bad) restaurants
      in Cape Town.

You can use here document to print multiple lines using your script as follows:


cat << EOF
This is a simple lookup program
for good (and bad) restaurants
in Cape Town.

This would produce following result:

This is a simple lookup program
for good (and bad) restaurants
in Cape Town.

The following script runs a session with the vi text editor and save the input in the file test.txt.


vi $filename <<EndOfCommands
This file was created automatically from
a shell script

If you run this script with vim acting as vi, then you will likely see output like the following:

[amrood]$ sh
Vim: Warning: Input is not from a terminal

After running the script, you should see the following added to the file test.txt:

[amrood]$ cat test.txt
This file was created automatically from
a shell script

Featured Post

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Hello fellow BSD lovers, I've created a patch process for patching openjdk6 for BSD (FreeBSD specifically), although I tried to keep all BSD versions in mind when creating my patch. Welcome to OpenJDK6 on BSD First let me start with a little …
Let's say you need to move the data of a file system from one partition to another. This generally involves dismounting the file system, backing it up to tapes, and restoring it to a new partition. You may also copy the file system from one place to…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…

632 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question