A resource for self-guided learning
  • Home
  • Getting Started
  • Software Installs
  • Learning Resource List
  • Domain-Specific Series

Introducing the Shell

  • The Unix Shell
    • Series Introduction
    • Installation and Resources
    • Introducing the Shell
    • The File System
    • Patterns, Filters, and Pipes
    • Variables and Loops
    • Bash Scripting

  • R Data Analysis and Visualization
    • Series Introduction
    • Installation and Resources
    • Introduction to R and RStudio
    • Starting with data
    • Manipulating, analyzing, and exporting data with tidyverse
    • Data visualization with ggplot2

On this page

  • What is the shell?
  • Why use the shell?
  • Accessing the Shell
  • Using the shell
  • Command syntax
    • Alter Command Behavior
  • Getting help
  • Making sense of errors

Introducing the Shell

What is the shell?

The shell (also known as the command-line) is a program that allows us to tell the computer what to do by giving it a “command” Figure 1a.

Tip

Other names for the shell are terminal, Bash, UNIX command line, and more.

Another common way we tell the computer what to do is through the use of a “point and click” graphical user interface (GUI) approach Figure 1b.

(a) Command Line Interface (CLI)

(b) Graphical User Interface (GUI)

Figure 1: Different ways to interact with a computer


Why use the shell?

Isn’t pointing and clicking easier? Imagine you had the following task:

You have a directory with 10 .txt files.

Pull the first line from each file into a single new file. You should end up with a list of all of the first lines.

Take a look below to see the process for GUI and CLI.

  • GUI Steps
  • CLI Code
1. Create new file
2. Open file 1, copy line 1, paste into new file, close file 1.
3. Open file 2, copy line 1, paste into new file, close file 2.
4. Repeat 7 more times
head -n1 -q *.txt > new-file.txt

For this task, the GUI was tedious, time-consuming, and error-prone while the CLI was a single-command, quick, and relatively error proof.


Accessing the Shell

Let’s start using the shell. Open the shell (terminal) on your computer. Select the appropriate instructions below, based on your operating system.

  • Windows Instructions
  • macOS Instructions
  1. Go the the Start menu and select “All Apps”.
  2. Scroll down the list of applications and select the Git option.
  3. From the drop-down menu, select Git Bash.
  4. A terminal should open up.
  1. Open Finder and go to the Applications tab.
  2. Scroll down the list of applications and select Utilities.
  3. Select Terminal.
  4. A terminal should open up.

Using the shell

Once we open our terminal, the $ shows us that shell is ready for input.

Let’s see what day it is using the ls command.

  • ls stands for list
  • lists the objects in a location

In your terminal, type ls and press enter.

ls
count.sh
example.gtf
exercise-data
fastqc.sh
fastqc_step.sh
feature_counts.sh
index
metadata.txt
north-pacific-gyre
rnaseq
rnaseq.sh
simulated_reads
star.sh
thesis
trim.sh
trim_fq.sh

Before we learn more commands, let’s learn about the structure of commands.


Command syntax

Commands follow a general syntax
command option/flag argument

  • command – the main command
  • option/flag – modifies the behavior of the command, often optional
  • argument – the source and/or target of the command, sometimes optional
Tip
  • Options use either - or -- to signal their usage.
  • Arguments can be either a target (as in the ls command) or both a source and target (as in the mv command)

Alter Command Behavior

Let’s change the way ls command behaves by providing a value for the option.

In your terminal, type ls -F and press enter.

ls -F
count.sh
example.gtf
exercise-data/
fastqc.sh
fastqc_step.sh
feature_counts.sh
index/
metadata.txt
north-pacific-gyre/
rnaseq/
rnaseq.sh
simulated_reads/
star.sh
thesis/
trim.sh
trim_fq.sh

This -F option/flag returns the output in a different format, with a / following directories and @ preceeding symbolic links.


Getting help

To better understand command usage and their options we can use the following (depending on the command).

Method of getting help Description Example
--help or -h option/flag Displays help menu for the command/program ls --help
man command Displays the manual for the command/program in-depth man ls

(a) Using the --help flag

(b) Using the man command

Figure 2: Different ways to get help with a command.


Making sense of errors

The shell provides (usually) helpful and informative error messages.

For example, if you look closely at the ls --help example above, you’ll see that the usage of --help actually resulted in an error (see below).

ls: unrecognized option `--help'
usage: ls [-@ABCFGHILOPRSTUWabcdefghiklmnopqrstuvwxy1%,] [--color=when] [-D format] [file ...]

Q&A: What is the error above telling us?

Click here for the answer
  1. --help is an unrecognized option
  2. The correct usage and options for ls
Installation and Resources
The File System
Cookie Preferences