Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm working my way through William Shotts Jr.'s great The Linux Command Line on my Mac OSX 10.7.5 system. 90% of the linux that Shotts covers is close enough to Darwin that I can figure out or GTEM to figure out what's going on. I've made it to chapter 27 on "Writing Shell Scripts" and am getting hung up creating "here files" within a function.

I get an syntax error: unexpected end of file error when I include the following function:

report_uptime () {
  cat <<- _EOF_
    <H2>System Uptime</H2>

The error goes away if I use the following function placeholder:

report_uptime () {

Also, elsewhere in the script, outside of a function I use the cat << _EOF_ format to create a "here file" with no trouble:

cat << _EOF_

If anyone has any idea what I'm doing wrong I would be grateful!

share|improve this question
The EOF token must be the first thing on the line, without any leading white spaces. – Ярослав Рахматуллин Nov 24 '12 at 19:58
That worked for me, @Ярослав_Рахматуллин. I don't think that the [-] is an option for Darwin's here files. cat << _EOF_ not cat <<- _EOF_ – BenU Nov 24 '12 at 20:32
that's a shame. – Ярослав Рахматуллин Nov 24 '12 at 22:07
up vote 3 down vote accepted

If the redirection operator is ‘<<-’, then all leading tab characters are stripped from input lines and the line containing delimiter. This allows here-documents within shell scripts to be indented in a natural fashion.

If that does not work as documented, try moving the EOF token to the very beginning of the line (remove all white spaces).

share|improve this answer
This should work regardless of the system, unless the bash version is broken on your system. – Ярослав Рахматуллин Nov 24 '12 at 20:15
Thanks @Ярослав_Рахматуллин Though adding the - to << didn't solve my problem, removing the whitespace before the _EOF_ did. Perhaps "here documents" with cat << works in Darwin but not cat <<-. It's a shame since <<- is so useful. Move your comment into your answer and I will accept it. Addendum: bash may in fact be broken. Possibly broken by me, possibly Mac's Darwin's limitations... – BenU Nov 24 '12 at 20:17
@BenU, note that the man page is very specific here: tab characters not whitespace – glenn jackman Nov 25 '12 at 4:41
@glennjackman Using spaces instead of tabs could indeed be the source of my problem. I've arranged my text editor to use two spaces instead of tab by default. Thanks! – BenU Nov 25 '12 at 4:47

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .