3 ms·
I prefer to use shell scripts for that, this works pretty well and is very readable: cat >README <<'EOF' This is a README EOF # This is a comm
by theamk 6y ago
I prefer to use shell scripts for that, this works pretty well and is very readable:
cat >README <<'EOF'
This is a README
EOF
# This is a comment
mkdir -p myProject
cat >myproject/index.js <<'EOF'
console.log("Hello world")
EOF
it does not require any tools to extract either.
(and if you omit quotes around EOF, you can do substitutions)
MY_EMAIL=put-your-email-here
cat >config <<EOF
email=$MY_EMAIL
EOF
- thaumaturgy 6y agoI use this pattern a lot along with a tool I built for doing server deployments and administration using plain old shell scripts and ssh (golem: https://github.com/robsheldon/golem/ https://github.com/robsheldon/golem/). There are two caveats: First, if there's any chance at all that the heredoc may contain a $, or a `, or possibly some other shell-magical characters, then you have to use a single-quoted heredoc: cat <<'EOF'... This means that if you want to do variable interpolation, like you're doing, then you need something that looks like: cat <<'EOF' | sed -e "s/\\\$username/$username/g" -e "s/\\\$my_email/$my_email/g" ... It looks yucky and unwieldy at first, but I've found that it's nice to be able to see at the top of the heredoc exactly what's getting replaced and what values it needs. Second, if root privileges are required to write the file, then you need to use `tee`, because you can't sudo an output redirection: cat <<'EOF' | sed -e "s/\\\$username/$username/g" | sudo tee /path/to/file >/dev/null After using it for a while, I've found I really like this pattern for managing configuration file templates. You're right though that something like Stamp could be built using standard shell tools if someone were so inclined.
- waheoo 6y agohttps://www.gnu.org/software/gettext/manual/html_node/envsubst-Invocation.html https://www.gnu.org/software/gettext/manual/html_node/envsub...