Using The Unix Shell

You can do pretty much everything with Python! Still, there’s a lot of value in becoming proficient on the command line. A well-crafted bash command or script can save hours that you would otherwise spend on coding it yourself (or doing stuff manually!).

The Unix philosophy

The Unix command-line has been around for decades, and it’s still going strong. Part of that success comes from the Unix philosophy:

  • Write programs that do one thing and do it well.
  • Write programs to work together.
  • Write programs to handle text streams, because that is a universal interface.

When working with the command line, it’s good to keep this philosophy in mind. You’ll find out that there’s always a combination of tools that does exactly what you want. The art is finding them and combining them correctly.

Linux commands and scripting

I’ll focus mostly on Linux, but what you’re about to learn applies one-on-one to other Unix-like operating systems, like macOS or the BSDs. The examples use Bash, the default shell on most Linux distributions. Since 2019, macOS uses zsh by default. The commands in this chapter work the same in zsh, so Mac users can follow along without switching shells. On Windows, you can get a real Linux shell with WSL.

I recommend you start by learning the most used, most needed Linux commands. After that, you can learn how to write Bash scripts, for example, to automate parts of your Python development process. These are all the articles in this chapter:

  • Basic Linux commands: the ten commands you’ll use every day, like ls, cd, cp and rm, plus how to reuse your shell history.
  • Bash scripts: write your own scripts with variables, arguments, loops and conditions.
  • The find command: search for files and directories, and do something with the results.
  • Unix pipes: connect commands with the | character, including your own Python scripts.
  • Process JSON with jq: filter, transform and pretty-print JSON on the command line.
  • Bash multiprocessing: run commands in parallel with xargs.

Related articles

Leave a Comment