Skip to content

Instantly share code, notes, and snippets.

@wheelq
Created February 8, 2021 20:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wheelq/ee7055d132d313ae6f219a4978e35a6c to your computer and use it in GitHub Desktop.
Save wheelq/ee7055d132d313ae6f219a4978e35a6c to your computer and use it in GitHub Desktop.
Heredoc hacks

variable substitution, leading tab retained, overwrite file, echo to stdout

tee /path/to/file <<EOF
${variable}
EOF

no variable substitution, leading tab retained, overwrite file, echo to stdout

tee /path/to/file <<'EOF'
${variable}
EOF

variable substitution, leading tab removed, overwrite file, echo to stdout

tee /path/to/file <<-EOF
    ${variable}
EOF

variable substitution, leading tab retained, append to file, echo to stdout

tee -a /path/to/file <<EOF
${variable}
EOF

variable substitution, leading tab retained, overwrite file, no echo to stdout

tee /path/to/file <<EOF >/dev/null
${variable}
EOF

the above can be combined with sudo as well

sudo -u USER tee /path/to/file <<EOF
${variable}
EOF
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment