Heredoc

Useful multiline string that acts like another file.

  • Supress tabs by prepending delimiter with a -
  • Supress variable expansion by quoting delimiter "EOF"
[COMMAND] <<[-] 'DELIMITER'
  Line 1
  Line 2
  ...
DELIMITER

Create a file inline with a heredoc

# create a post-merge git hook that call this script
cat <<EOF > .git/hooks/post-merge
#!/bin/sh
$0
EOF

Support tab indentations Heredoc

# indented using tabs
[ -f .git/hooks/post-merge ] || {
    echo "Setup post-merge git hook ..."
    cat <<-EOF > .git/hooks/post-merge
  	echo hi
    chmod +x .git/hooks/post-merge
}
 
# Indented help function with literal tab characters
while read -r; do printf '%s\n' "$REPLY" done <<-EOF
	Usage: ${0##*/} -h -v -n
 
	Lorem ...
EOF

Run SQL Script in docker container

docker exec -i stockis-destillerie-db sh -c 'exec mysql -uroot -p"1234" stockisdes' <<EOF
show tables;
EOF