Sending Multiple Lines of Input to a Program

Posted October 20, 2003 by Rex in UNIX shell scripting

Using a “here document,” it is possible to send multiple lines of input to a program from a shell script.

A here document uses the << I/O redirector followed by a code. The subsequent lines are redirected to the program until the specified code. The basic syntax is as follows: program << CoDe Line of input More input extra input CoDe

The code needs to be something that would not occur in the text. This technique is useful for working with interactive programs like ftp. In the following example, a file specified in a shell variable ($filename) is retrieved from an ftp server:

ftp -n << TheEnd user username password cd directory/subdir get $filename quit TheEnd

The Conversation

Follow the reactions below and share your own thoughts.