How to use a heredoc inside a bash function?

Solution 1:

Your code looked fine to me, so I copied it line for line and it worked for me on:

  • OSX 10.7, bash 3.2.48(1)-release (x86_64-apple-darwin11).
  • Ubuntu 10.04.3, bash 4.1.5(1)-release (x86_64-pc-linux-gnu)

Is it possible for you to retype the file by hand just to make sure you aren't missing or adding spaces/tabs/etc?

Also, how are you executing it? I did a chmod +x test.sh and then ./test.sh

EDIT: I received the same error as you when I added a space at the beginning of the line with the closing HEREDOC.

Further Note: A trailing space behind the closing HEREDOC can also cause this