Essential tips for scripting in Bash and Python

In my last article, Developers want scripts and tools — not “how-to” guides, I wrote about the importance of providing runnable script files in your software-related technical writing, instead of just listing out a series of commands that the reader has to copy and paste in order to accomplish a task.

In this article, I’ll walk you through some tips that have been indispensable to me while writing Bash and Python scripts, and distributing those scripts to other engineers.

Note: this guide is targeted towards Linux users. In future articles, I’ll be writing about PowerShell scripting in Windows (and why it’s awesome).

Bash scripts

For simple scripts that only consist of a few commands and maybe a few optional inputs, I like to use Bash, since it requires very little boilerplate and the syntax is extremely minimal, requiring few keystrokes and therefore saving lots of time.

sudo bash -c 'cd /usr/local/bin && wget -qO-    https://gist.githubusercontent.com/bduffany/a0cb3a8be0364dae91804ac510670e81/raw/7717ce1911fff3ba19dabc7f4acbb03cd5beda1f/new-script.sh > new-script && chmod +x new-script'

If your Bash script starts getting too complicated (you find yourself needing to use loops, arrays, maps, or command line argument parsing), it’s probably time to switch to Python!

Python scripts

I use Python scripts whenever I need to parse arguments (which is a terrible experience in Bash) or if I need to use complex data structures (the syntax for dealing with maps and arrays in Bash is atrocious!).

This article is not intended to be a complete guide to Python, but here are a few basic tips for writing command-line tools in Python:

Hopefully you found these tips to be useful. If you have any other tips, I’d love to hear them. Thanks for reading!

Working towards epic levels of developer productivity at buildbuddy.io

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store