ZhangZhihui's Blog  

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:
    zzh@ZZHPC:~$ wc -l << EOF
    > This is a simple lookup program for good (and bad) restaurants in Cape Town.
    > EOF
    1
  • 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.
posted on 2022-10-24 11:50  ZhangZhihuiAAA  阅读(14)  评论(0编辑  收藏  举报