The Unix Shell

SciencesPo Intro To Programming 2024

Florian Oswald and Software Carpentry

29 April, 2024

Intro

Question

  • What is a command shell and why would I use one?

Objectives

  • Explain how the shell relates to the keyboard, the screen, the operating system, and users’ programs.
  • Explain when and why command-line interfaces should be used instead of graphical interfaces.

Do You GUI?

What’s a GUI

A Graphical User Interface (GUI) lets the user interact by clicking with a mouse and using menus.

  • I love 😍 a good Graphical User Interface (GUI)
  • But. Bad things can happen.
  • ☠️

Bad.

No More than 65,536 Rows

No Growth with High Debt?

Or Do you CLI?

What’s a CLI

A Command Line Interface (CLI) allows interaction via (text) commands.

  • CLIs can collect commands somewhere - reproducible
  • But one has to learn a language.
  • They are great for long, repetitive tasks.
  • It’s often the only way to interact with high-performance computing. 👉 show DANTE

The Shell

  • The shell is a program where we can type in commands and get output.
  • We often use very simple programs - good for just one thing.
  • There is tremendous power in combining those little programs.
  • It’s a platform approach to an Operating System.

Unix is a Platform

A protocol and many small program who interact according to the rules with each other

lewing@isc.tamu.edu Larry Ewing and The GIMP, Attribution, via Wikimedia Commons

Go! 🚀

  1. Open your terminal! (GitBash on Windows)
  2. You should see something like
$

which is called the prompt.

  1. You don’t have to type the $!
  2. next to it, you see a cursor.

First command: ls

  • type ls and hit enter
  • you see something like this as output:
Desktop     Downloads   Movies      Pictures
Documents   Library     Music       Public
  • By default, the terminal opens in your home directory.
  • ls lists the content of that directory.

First Error!

Caution

  • If you mistype a command, or look for a program that is not installed, you get an error. Like:
bash-3.2$ ks
bash: ks: command not found
  • Look for a spelling mistake (it’s ls not ks)
  • Or install the required program.

Nelle’s Pipeline: A Typical Problem

  • Nelle Nemo is a marine biologist. 🌊 🐡
  • Just sampled gelatinous marine life in the Great Pacific Garbage Patch.
  • From 1520 samples she obtained measures of the relative abundance of 300 proteins.
  • Her supervisor, Professor Jones, handed over to her a program called goostats.sh.
  • goostats.sh needs to be run on each of the 1520 samples.
  • Paper needs to be ready by the end of the month.

Battle Plan

  • Using a GUI to run goostats.sh, Nelle will have to use her mouse to select and open a file 1520 times.
  • If goostats.sh takes 30 secs to run, this will take more than 12 hours of Nelle’s active time.
  • With the help of the shell, Nelle could make her computer go through that list of files instead.
  • Bonus : she will have a working pipeline, that can be re-run each time she wants to add data or reproduce previous output.

What Does Nelle Need

Nelle has needs to learn the following things:

  • navigate to a file/directory
  • create a file/directory
  • check the length of a file
  • chain commands together
  • retrieve a set of files
  • iterate over files
  • run a shell script containing her pipeline

And we will be right next to her. 🙂

Key Points

  • A shell is a program whose primary purpose is to read commands and run other programs.
  • This lesson uses Bash, the default shell in many implementations of Unix.
  • Programs can be run in Bash by entering commands at the command-line prompt.
  • The shell’s main advantages are its high action-to-keystroke ratio, its support for automating repetitive tasks, and its capacity to access networked machines.
  • The shell’s main disadvantages are its primarily textual nature and how cryptic its commands and operation can be.
  • ChatGPT can help you write Bash scripts, still it is fundamental to know the basics.