Is it possible to insert a heredoc inside a bash function?
The trivial sample below results in an error: ./heredoc_in_function.sh: 10: Syntax error: end of file unexpected (expecting "}")
#!/bin/sh
my_function () {
cat <<HEREDOC
heredoc contents
HEREDOC
}
my_function
Environment:
- Ubuntu 11.04 (GNU/Linux 2.6.38-8-server x86_64)
- GNU bash, version 4.2.8(1)-release (x86_64-pc-linux-gnu)
Best Answer
Your code looked fine to me, so I copied it line for line and it worked for me on:
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