How can I write a heredoc to a file in Bash script?

前端 未结 9 999
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-11-22 12:33

How can I write a here document to a file in Bash script?

相关标签:
9条回答
  • 2020-11-22 12:56

    Instead of using cat and I/O redirection it might be useful to use tee instead:

    tee newfile <<EOF
    line 1
    line 2
    line 3
    EOF
    

    It's more concise, plus unlike the redirect operator it can be combined with sudo if you need to write to files with root permissions.

    0 讨论(0)
  • 2020-11-22 13:00

    To build on @Livven's answer, here are some useful combinations.

    1. variable substitution, leading tab retained, overwrite file, echo to stdout

      tee /path/to/file <<EOF
      ${variable}
      EOF
      
    2. no variable substitution, leading tab retained, overwrite file, echo to stdout

      tee /path/to/file <<'EOF'
      ${variable}
      EOF
      
    3. variable substitution, leading tab removed, overwrite file, echo to stdout

      tee /path/to/file <<-EOF
          ${variable}
      EOF
      
    4. variable substitution, leading tab retained, append to file, echo to stdout

      tee -a /path/to/file <<EOF
      ${variable}
      EOF
      
    5. variable substitution, leading tab retained, overwrite file, no echo to stdout

      tee /path/to/file <<EOF >/dev/null
      ${variable}
      EOF
      
    6. the above can be combined with sudo as well

      sudo -u USER tee /path/to/file <<EOF
      ${variable}
      EOF
      
    0 讨论(0)
  • 2020-11-22 13:02

    Read the Advanced Bash-Scripting Guide Chapter 19. Here Documents.

    Here's an example which will write the contents to a file at /tmp/yourfilehere

    cat << EOF > /tmp/yourfilehere
    These contents will be written to the file.
            This line is indented.
    EOF
    

    Note that the final 'EOF' (The LimitString) should not have any whitespace in front of the word, because it means that the LimitString will not be recognized.

    In a shell script, you may want to use indentation to make the code readable, however this can have the undesirable effect of indenting the text within your here document. In this case, use <<- (followed by a dash) to disable leading tabs (Note that to test this you will need to replace the leading whitespace with a tab character, since I cannot print actual tab characters here.)

    #!/usr/bin/env bash
    
    if true ; then
        cat <<- EOF > /tmp/yourfilehere
        The leading tab is ignored.
        EOF
    fi
    

    If you don't want to interpret variables in the text, then use single quotes:

    cat << 'EOF' > /tmp/yourfilehere
    The variable $FOO will not be interpreted.
    EOF
    

    To pipe the heredoc through a command pipeline:

    cat <<'EOF' |  sed 's/a/b/'
    foo
    bar
    baz
    EOF
    

    Output:

    foo
    bbr
    bbz
    

    ... or to write the the heredoc to a file using sudo:

    cat <<'EOF' |  sed 's/a/b/' | sudo tee /etc/config_file.conf
    foo
    bar
    baz
    EOF
    
    0 讨论(0)
  • 2020-11-22 13:05

    When root permissions are required

    When root permissions are required for the destination file, use |sudo tee instead of >:

    cat << 'EOF' |sudo tee /tmp/yourprotectedfilehere
    The variable $FOO will *not* be interpreted.
    EOF
    
    cat << "EOF" |sudo tee /tmp/yourprotectedfilehere
    The variable $FOO *will* be interpreted.
    EOF
    
    0 讨论(0)
  • 2020-11-22 13:05

    If you want to keep the heredoc indented for readability:

    $ perl -pe 's/^\s*//' << EOF
         line 1
         line 2
    EOF
    

    The built-in method for supporting indented heredoc in Bash only supports leading tabs, not spaces.

    Perl can be replaced with awk to save a few characters, but the Perl one is probably easier to remember if you know basic regular expressions.

    0 讨论(0)
  • 2020-11-22 13:09

    I like this method for concision, readability and presentation in an indented script:

    <<-End_of_file >file
    →       foo bar
    End_of_file
    

    Where →        is a tab character.

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