Marco Bravo

Logo

Marco Bravo's DEV Profile If you don’t know, ask – if you know, share! ~ opensource mindset Baseball ball image


➠ "We may not have control over our circumstances, but we do have control over our minds"
➠ What do I want to do next?
Cloud and rainbow image

View My GitHub Profile

13 March 2020

Writing Safe Shell Scripts

by Marco Bravo

Dodo image

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.

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.

Full article

tags: sysadmin - tools - bash - automation