Search code examples
bashshellubuntuwindows-subsystem-for-linuxheredoc

Syntax error: end of file unexpected (expecting "}")


I am running the following script within a longer html file

report_home_space () {
 cat <<- _EOF_
 <h2>Home Space Utilization</h2>
 <pre>$(du -sh /home/*)</pre>
 _EOF_
 return
}
cat <<- _EOF_
<html>
*
  other html here
*
</html>
_EOF_

Why I am getting end of file unexpected what should I be supposed to do here ? I have also tried cat << EOF but it does not work within my functions, the problem arises only when I insert Here Document inside a function. (I am currently using the bash shell)


Solution

  • If you are going to indent the here-doc delimiter, it must be with tab characters. In your code, everything until the last line of the script is part of the first here-doc.

    report_home_space () {
      cat <<- _EOF_
            <h2>Home Space Utilization</h2>
            <pre>$(du -sh /home/*)</pre>
            _EOF_
      return
    }
    

    Here, the 8 spaces used to indent the two lines of the here document and the terminator should be replaced with a literal tab character.