Heredocs#
Bash Here Documents (short heredocs) are a great way to pass multi-line input to a command.
I mostly use them to initialize files when writing documentation or Dockerfiles, e.g.
some_env_var=foo
cat <<EOF > /some/file.yaml
nested:
value: $some_env_var
EOF
with variable substitution#
foo=42
cat <<EOF
The answer is $foo.
EOF
returns
The answer is 42.
without variable substitution#
foo=42
cat <<'EOF'
The answer is $foo.
EOF
returns
The answer is $foo.
using another delimiter#
cat <<MY_CUSTOM_DELIMITER
foo
bar
baz
MY_CUSTOM_DELIMITER
use any command#
Using cat
is great to print to STDOUT or redirect to a file, but heredocs are universal, e.g.
psql <<EOF
SELECT * FROM users;
EOF
python <<'EOF'
import sys
print(sys.version)
EOF