Skip to content

Instantly share code, notes, and snippets.

@webern
Created August 14, 2020 00:18
Show Gist options
  • Save webern/c213dc00c8bbb79be98ee7e8179561e1 to your computer and use it in GitHub Desktop.
Save webern/c213dc00c8bbb79be98ee7e8179561e1 to your computer and use it in GitHub Desktop.
I can never remember how to do heredocs
#!/usr/bin/env bash
# heredoc with shell expansion
cat << EOF
The current working directory is: $PWD
You are logged in as: $(whoami)
EOF
# heredoc with shell expansion into a variable
myvar=$(cat << EOF
The current working directory is: $PWD
You are logged in as: $(whoami)
EOF
)
echo "${myvar}"
# heredoc without shell expansion (the delimiter has quotes)
cat <<- "EOF"
The current working directory is: $PWD
You are logged in as: $(whoami)
EOF
# heredoc without shell expansion (the delimiter has quotes)
myvar2=$(cat <<- "EOF"
The current working directory is: $PWD
You are logged in as: $(whoami)
EOF
)
echo "${myvar2}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment