Heredoc Multi Line String
Heredoc Syntax
Heredoc (<<DELIMITER) allows multi-line string input in shell:
- Content between delimiters is passed as input to the command
- Variables are expanded by default
- Use single quotes around first delimiter usage (
<<'DELIMITER') to prevent expansion.
- Use single quotes around first delimiter usage (
- Delimiter can be any word (commonly
EOF,END, etc.) ${VAR:?}expands variable or exits with error if unset
Common pattern: cat > file.txt <<EOF to write multi-line content to a file.
Example
cat > ./.tmp/bucket-policy.json <<__EOF__
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "PublicReadGetObject",
"Effect": "Allow",
"Principal": "*",
"Action": "s3:GetObject",
"Resource": "arn:aws:s3:::${BUCKET_NAME:?}/*"
}
]
}
__EOF__