Live data from Hacker News

Defensive BASH programming

kfirlavi.com

1–10 of 53 posts

Re: Defensive BASH programming

#2
I think the best "defensive" piece of advice you left out that everyone abuses is never pipe `find` results to `xargs.` One should always do `find ... -print0` to a read-while loop because of filenames with whitespace.

Re: Defensive BASH programming

#4

I think the best "defensive" piece of advice you left out that everyone abuses is never pipe `find` results to `xargs.` One should always do `find ... -print0` to a read-while loop because of filenames with whitespace.

And you also want to prevent `while` loop in shell script. See http://unix.stackexchange.com/q/169716/38906 for more details.

Re: Defensive BASH programming

#6

I think the best "defensive" piece of advice you left out that everyone abuses is never pipe `find` results to `xargs.` One should always do `find ... -print0` to a read-while loop because of filenames with whitespace.

`xargs -0' was designed to properly handle the output of `find -print0' -- no while loop needed.

`find -print0 | xargs -0 grep some_pattern'

Re: Defensive BASH programming

#9
post #5

All these recommendation are excellent (except omitting the necessary obsessive quoting of all variables everywhere (just in case they contain a space). I've been enjoying BATS [0] for my bash testing. [1] https://github.com/sstephenson/bats

BATS looks great, but it might not be maintained any more: https://github.com/sstephenson/bats/issues
Post reply on HN