Snacker News

The Unix shell as a biologist sees it

September 10, 2019

This post began as a simple question: “How do I make this script I just wrote accessible on the command line?” The answer took us on a journey. Along the way, we learned a lot about what exactly the “command line” is, and how it’s useful for humans doing research and engineering in biology.

What is a shell, anyway?

The simple answer to the question posed above is: “add the binary to your PATH environment variable. But what is the PATH environment variable? Where is it set? What uses it? What’s the command line anyway?

It turns out that “the command line” is a program just like so many others than run on my computer. On the computers most folks use for doing biology, the “command line” is called a “shell”. A shell is a program that runs in an endless loop, always asking for input, reading it, and printing out the result, and then asking for input again. The shell’s only purpose is to ask “What do you want me to do?” and then do it.

The string of characters the shell types when you open it is called the prompt. Since one particular shell—-Bash—-is the commonest, and Bash uses $ as a prompt, you’ll usually see lines of code you’re supposed to type into your terminal written as


Doesn’t that look inviting? Type a command, press Enter, and the shell executes the command.

So where do all the commands come from?

The short answer is that your shell can be configured to look anywhere you want for commands that it can execute. In the case of Bash (the most common shell), the environment variable PATH holds all the places the shell looks for commands.

A simple way to make software that you develop locally available in your shell as first-class functions is to add the directory containing the binaries to your PATH environment variable.

If you have a project directory containing a build directory where built binaries are placed, then you can add that directory to the PATH variable such that all the binaries within are accessible in the shell. For example, your project directory is as follows.

├── src
│   ├── main.c
│   ├── lib.c
│   └── test.c
└── build
    └── bin
        └── my_app

Then you can add the following to your .bashrc to make the binaries accessible at the command line. You can do this as many times as you wish.

export PATH="/path/to/build/bin:$PATH"
# where this is the absolute path to your binaries