How does “cat << EOF” work in bash?

前端 未结 9 1305
深忆病人
深忆病人 2020-11-22 12:24

I needed to write a script to enter multi-line input to a program (psql).

After a bit of googling, I found the following syntax works:

c         


        
9条回答
  •  萌比男神i
    2020-11-22 13:11

    POSIX 7

    kennytm quoted man bash, but most of that is also POSIX 7: http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_07_04 :

    The redirection operators "<<" and "<<-" both allow redirection of lines contained in a shell input file, known as a "here-document", to the input of a command.

    The here-document shall be treated as a single word that begins after the next and continues until there is a line containing only the delimiter and a , with no characters in between. Then the next here-document starts, if there is one. The format is as follows:

    [n]<

    where the optional n represents the file descriptor number. If the number is omitted, the here-document refers to standard input (file descriptor 0).

    If any character in word is quoted, the delimiter shall be formed by performing quote removal on word, and the here-document lines shall not be expanded. Otherwise, the delimiter shall be the word itself.

    If no characters in word are quoted, all lines of the here-document shall be expanded for parameter expansion, command substitution, and arithmetic expansion. In this case, the in the input behaves as the inside double-quotes (see Double-Quotes). However, the double-quote character ( '"' ) shall not be treated specially within a here-document, except when the double-quote appears within "$()", "``", or "${}".

    If the redirection symbol is "<<-", all leading characters shall be stripped from input lines and the line containing the trailing delimiter. If more than one "<<" or "<<-" operator is specified on a line, the here-document associated with the first operator shall be supplied first by the application and shall be read first by the shell.

    When a here-document is read from a terminal device and the shell is interactive, it shall write the contents of the variable PS2, processed as described in Shell Variables, to standard error before reading each line of input until the delimiter has been recognized.

    Examples

    Some examples not yet given.

    Quotes prevent parameter expansion

    Without quotes:

    a=0
    cat <

    Output:

    0
    

    With quotes:

    a=0
    cat <<'EOF'
    $a
    EOF
    

    or (ugly but valid):

    a=0
    cat <

    Outputs:

    $a
    

    Hyphen removes leading tabs

    Without hyphen:

    cat <a
    EOF
    

    where is a literal tab, and can be inserted with Ctrl + V

    Output:

    a
    

    With hyphen:

    cat <<-EOF
    a
    EOF
    

    Output:

    a
    

    This exists of course so that you can indent your cat like the surrounding code, which is easier to read and maintain. E.g.:

    if true; then
        cat <<-EOF
        a
        EOF
    fi
    

    Unfortunately, this does not work for space characters: POSIX favored tab indentation here. Yikes.

提交回复
热议问题