It’s something that can bite you, but its not that hard to protect against. For example (taken from sandstorm.io’s script):
# We wrap the entire script in a big function which we only call at the very end, in order to
# protect against the possibility of the connection dying mid-script. This protects us against
# the problem described in this blog post:
# http://blog.existentialize.com/dont-pipe-to-your-shell.html
_() {
set -euo pipefail
Explanation: echo "$(…)" makes sure the whole file is downloaded before it is passed to the shell. In the curl flags, -L follows redirects. And -fsS suppresses error output on STDOUT (which would be executed by bash) and shows it on STDERR instead.
It’s something that can bite you, but its not that hard to protect against. For example (taken from sandstorm.io’s script):
You can avoid this problem, and some other safety problems, by piping to
bashwith this short template:Explanation:
echo "$(…)"makes sure the whole file is downloaded before it is passed to the shell. In thecurlflags,-Lfollows redirects. And-fsSsuppresses error output on STDOUT (which would be executed bybash) and shows it on STDERR instead.Doesn’t it need a terminator (e.g. EOL, semicolon) before executing the command? Or does
curlauto-insert an EOF when the connection is dropped?It executes when it gets to the end of the stream:
As you can see,
printf 'echo hello'doesn’t end with a line-break or semi-colon, butshexecutes the result just fine.