Multiline syntax for piping a heredoc; is this portable?

前端 未结 4 1507
北恋
北恋 2020-12-12 14:54

I\'m familiar with this syntax:

cmd1 << EOF | cmd2
text
EOF

but just discovered that bash allows me to write:

cmd1 &         


        
相关标签:
4条回答
  • 2020-12-12 15:09

    Hmm, I suppose yes, according to the test in bash in POSIX mode:

    $ bash --posix
    $ cat <<EOF |
    > ahoj
    > nazdar
    > EOF
    > sed 's/a/b/'
    bhoj
    nbzdar
    
    0 讨论(0)
  • 2020-12-12 15:12

    Yes, the POSIX standard allows this. According to the 2008 version:

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

    And includes this example of multiple "here-documents" in the same line:

    cat <<eof1; cat <<eof2
    Hi,
    eof1
    Helene.
    eof2
    

    So there is no problem doing redirections or pipes. Your example is similar to something like this:

    cat file |
    cmd
    

    And the shell grammar (further down on the linked page) includes these definitions:

    pipe_sequence    :                             command
                     | pipe_sequence '|' linebreak command
    
    newline_list     :              NEWLINE
                     | newline_list NEWLINE
                     ;
    linebreak        : newline_list
                     | /* empty */
    

    So a pipe symbol can be followed by an end-of-line and still be considered part of a pipeline.

    0 讨论(0)
  • 2020-12-12 15:17

    Yes it's in the POSIX shell grammar. You can also have more than one here-doc for the same command (some other examples use two cat invocations, but this works as well):

    cat <<EOF1 <<EOF2
    first here-doc
    EOF1
    second here-doc
    EOF2
    

    This is contrived (using 2 here-docs for stdin), but if you think of providing input for different file descriptors it immediately makes sense.

    There's also the possibility to drop the cat entirely. Why not make the here-document directly available to cmd:

    cmd << EOF
    input
    here
    EOF
    
    0 讨论(0)
  • 2020-12-12 15:17

    Hi, check this, for example

    #!/bin/sh
    ( base32 -d | base64 -d )<<ENDOFTEXT
    KNDWW42DNNSHS5ZXPJCG4MSVM5MVQVT2JFCTK3DELBFDCY2IIJYGE2JUJNHWS22LINVHQMCMNVFD
    CWJQIIZVUV2JOVNEOVJLINTW6PIK
    ENDOFTEXT
    

    regards

    0 讨论(0)
提交回复
热议问题