D

David Pashley

davidpashley.com · Personal blog · on HN since 2010

David Pashley is a sysadmin-minded developer whose best-known post explains how to write robust Bash shell scripts.

What HN saysCommenters point to his robust shell scripts article as the go-to reference, especially for using set -e and set -u in Bash scripts.
20HN people shared it
10linked it in comments
10stories submitted
157story points
3 years agolast shared
2010people per year2026

Most-shared posts

  1. 1
    Writing Robust Bash Shell Scripts
    13 people · 68 points · first shared 2013 · 4 comments on HN
  2. 2
    Writing Robust Bash Shell Scripts
    4 people · 60 points · first shared 2010 · 21 comments on HN
  3. 3
    Livejournal mozilla bug
    2 people · first shared 2010
  4. 4
    Bad Password Policies
    1 people · 29 points · first shared 2014 · 43 comments on HN

What HN people wrote when they linked it

arnehormann · 2013-08-14
Huh, no mention of "set -e -u"? Do yourself a favor and follow this, too: http://www.davidpashley.com/articles/writing-robust-shell-scripts/
theoh · 2018-12-27
There are problems with the manipulation of binary data in bash, as discussed here: https://unix.stackexchange.com/questions/278639/how-can-i-work-with-bi… In particular, the value 0 can't be stored in a bash variable, which almost completely rules out the possibility of byte-by-byte processing of general binary data. It's probably possible with absurd hacks. Quite apart from that, error handling in bash is an awkward chore which few scripts get right. It's a very unappealing language for writing anything more than a five-line script. https://www.davidpashley.com/articles/writing-robust-shell-scripts/
wolfgang42 · 2017-03-23
To expound on this: "set -e" is short for "set -o errexit", that is, abort the script if a command returns with a non-zero exit code. "set -u" is short for "set -o nounset", that is, abort the script if a variable name is dereferenced when the variable hasn't been set. I also strongly suggest reading http://www.davidpashley.com/articles/writing-robust-shell-scripts/ for more things that you should be aware of when writing bash scripts.
westoque · 2015-05-22
You could say the same for scripts, languages, etc when not using safety precautions. I would look at the link from erikb's answer: http://www.davidpashley.com/articles/writing-robust-shell-scripts/
erikb · 2015-05-22
Here's a good article about how it should be done. It does not just say "use move", but it also explains how to backup the old state, how to use traps to remove your workaround code etc. http://www.davidpashley.com/articles/writing-robust-shell-scripts/
gruseom · 2011-01-02
Looking for more about this, I found a pretty good post: http://www.davidpashley.com/articles/writing-robust-shell-scripts.html
simonw · 2010-01-11
On further inspection, I don't trust your implementation at all. You're blacklisting CSS rules and attributes rather than whitelisting them. This means you wouldn't catch attacks like this one, for example: http://www.davidpashley.com/blog/computing/livejournal-mozilla-bug.htm… I also don't think you're the right thing when you DO find something that's not on the whitelist - you should be escaping it rather than stripping it (we couldn't have a discussion about your code using your system, since our XSS examples would be stripped). I'd suggest re-engineering to use a whitelist for everything.
j1elo · 2020-07-07
I learnt the same trick some years ago, from an article called Shell Scripts Matter: https://dev.to/thiht/shell-scripts-matter So I took some of the advice and tips offered in there, and wrote a template file to be used as a baseline when writing scripts for any project that might need one: https://github.com/j1elo/shell-snippets/blob/master/template.sh Other resources that I link in the readme of that repo, because they were a great guide to write better and more robust scripts, are: - Writing Robust Bash Shell Scripts: https://www.davidpashley.com/articles/writing-robust-shell-scripts/ - Common shell script mistakes: http://www.pixelbeat.org/programming/shell_script_mistakes.html - Bash Pitfalls: http://mywiki.wooledge.org/BashPitfalls - The Bash Hackers Wiki: https://wiki.bash-hackers.org/ EDIT: -for anyone who would like to read some actual examples- I have to manage a bunch of scripts so actually a slightly more up to date version of the template is put into practice by means of a common bash.conf file that then gets sourced by all scripts: https://github.com/Kurento/adm-scripts/blob/master/bash.conf.sh

Keyboard shortcuts

/Search
j kNext / previous blog
EnterOpen selected blog
vVisit the blog site
fFollow / unfollow
lSwitch grid / list view
dSwitch dark mode
g hHome
g pTop posts
g tTopics
g fFollowing
g lLiked
g yHistory
g qFAQ
EscBack / close