If you don’t know, ask – if you know, share! ~ opensource mindset
by Marco Bravo
Writing shell scripts leaves a lot of room to make mistakes, in ways that will cause your scripts to break on certain input, or (if some input is untrusted) open up security vulnerabilities. Here are some tips on how to make your shell scripts safer.
The simplest step is to avoid using shell at all. Many higher-level languages are both easier to write the code in in the first place, and avoid some of the issues that shell has. For example, Python will automatically error out if you try to read from an uninitialized variable (though not if you try to write to one), or if some function call you make produces an error.
set -euf -o pipefail
Conclusion When possible, instead of writing a “safe” shell script, use a higher-level language like Python. If you can’t do that, the shell has several options that you can enable that will reduce your chances of having bugs, and you should be sure to quote liberally.
tags: sysadmin - tools - bash - automation