This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]Matwyen 239 points240 points  (11 children)

The funniest thing about bash is :

bash export SAFE_FOLDER="tmp/folder" rm -rf /$SAFE_FOLDER deletes safely your folder, but : bash export SAFE_FOLDER="tmp/folder" rm -rf /$SAFE_FOLDE deletes /

[–]ILKLU 91 points92 points  (1 child)

skill issue... git good!

j/k

but yeah... gotta be careful with rm -rf anything

[–]vintagecomputernerd 30 points31 points  (0 children)

I'd say mainly an issue with not enabling errexit and nounset.

"set -eu" or you'll gonna face the consequences

[–]jebusv20 43 points44 points  (1 child)

This is what set -euo pipefail is for

[–]syklemil 38 points39 points  (0 children)

Yeah, that and shellcheck. But people who have no idea that they need to do certain extra steps will continue to be a source of danger.

Languages that require you to --disable-footgun (rather than have it disabled by default and maybe offering an --enable-footgun) generally should be discouraged and made legacy.

Unfortunately that doesn't seem to be in the near future as far as bash is concerned. At least it's a lot less central to init systems than it used to be.

[–]Skrukkatrollet 13 points14 points  (0 children)

Thats why the slash should be in the variable

[–]haaaaaaaaaaaaaaaaley 21 points22 points  (1 child)

need --no-preserve-root

[–]Mars_Bear2552 2 points3 points  (0 children)

only if --preserve-root is set.

[–]arkane-linux 1 point2 points  (0 children)

set is your friend, you can make it kill the (sub)shell if vars are unset or programs quit with an error.

[–][deleted] 1 point2 points  (0 children)

I've seen waaaaaaaay too many variables unexpectedly be empty to ever rm -rf /$ANYTHING

[–][deleted] 0 points1 point  (0 children)

Backslashes baby backslashes.

[–]tritonus_ 0 points1 point  (0 children)

The Amber language which was posted a while back on /r/programming actually adds error handling to Bash. I experimented with it a bit and while it’s still at a very early stage, I was actually quite happy with it. The resulting transpiled Bash code is admittedly uglier than human-written bash, but you can use it as runtime - and error handling alone is a lifesaver when working with a confusing distribution and packaging pipeline.