[Robelle] [SmugBook] [Index] [Prev] [Next]

Feed Input to Program with hereis Document

UNIX has a nice feature called the hereis document for specifying input to a program in a shell script. You could just use the < I/O redirection character, but then you would have to put the input in a file somehow and remember to remove it later. With hereis you include the input data as in-line text after the program name, eliminating the separate file and making the meaning more obvious. To do so, use the << operator followed by a terminating string that will not appear in your text:
    sort >file <<EndSortData
    bob
    sam
    bill
    EndSortData

Another advantage of this syntax is that variable and command substitution is done on the input data. Of course, many times you will not want variable substitution to occur. To suppress substitution, put a backslash in front of the terminator string. Otherwise, anything in your hereis document that starts with a "$" may be screwed up. In one case, a user did the following commands for an RJE job and the "$$" kept getting replaced with what appeared to be random numbers (it was actually the shell's PID, process id):

    cat > file <<EOF
    $$ADD ID=foo PASSWORD=bar
    EOF

Unfortunately, the C shell (csh) expects you to escape the terminator at the end of your list with backslash, while the Bourne and Korn shells (sh and ksh) do not:

    #! /bin/csh
    cat << \eof
    Smug book example of a three-line hereis document in csh.
    eof
    'e'of
    \eof


[Robelle] [SmugBook] [Index] [Unix] [Prev] [Next]