5

This question already has an answer here:

I have an unexpected behaviour- following snippet fail with 'unbound variable' error:

#!/bin/bash
set -u
<<EOF
a=a
b=$a
EOF

Tested on:
GNU bash, version 4.4.12(1)-release
GNU bash, version 4.2.46(2)-release

marked as duplicate by Jeff Schaller, Community Nov 24 '17 at 18:23

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

11

What you have defined with set -u is force an exit with an error message, if attempted to use undefined variable (The set builtin command).

The form of here-documents << with EOF is equivalent to as if double-quoting the words inside to allow the variables to be expanded (parameter expansion) by the shell (bash in this case; also subject to command substitution and arithmetic expansion). To avoid the expansion from happening quote the here-string with a single quote

set -u
<<'EOF'
a=a
b=$a
EOF

Not the answer you're looking for? Browse other questions tagged or ask your own question.