-
Malvika Sharan authoredMalvika Sharan authored
Introduction to the Unix shell
This work by Konrad Förstner, Toby Hodges, Holger Dinkel, and Frank Thommen, is licensed under a Creative Commons Attribution 4.0 International License.
Introduction to the Unix shell
Motivation and background
In this course you will learn the basics of how to use the Unix shell. Unix is a class of operating systems with many different flavors including well-known ones like GNU/Linux and the BSDs. The development of Unix and its shell (also known as command line interface) dates back to the late 1960s. Still, the concepts of this interface lead to very powerful tools. In the command line you can easily combine different tools into pipelines, avoid repetitive work and make your workflow reproducible. Knowing how to use the shell will also enable you to run programs that are only developed for this environment, which is the case for many bioinformatical tools.
The basic anatomy of a command line call
Running a tool in the command line interface follows a simple pattern. At first you have to write the name of the command (if it is not globally installed it's precise location needs to be given - we will get to this later). Some programs additionally require parameters. While the parameters are the requirement of the program the actual values we give for these are called arguments. There are two different ways how to pass those arguments to a program
- via keywords parameter (also called named keywords, flags or options) or via positional parameters. The common pattern looks like
this (
<>
indicates obligatory items,[]
indicates optional items):
<program name> [keyword parameters] [positional parameters]
An example is calling the program ls
, which lists the content of a
directory. You can simply call it without any argument
$ ls
or with one or more keyword argument
$ ls -l
$ ls -lh
or with one or more positional arguments
$ ls test_folder
or combining one or more keyword and positional arguments
$ ls -l test_folder
The result of a command is written usually to the so called standard output of the shell, which is the screen shown to you. We will later learn how to redirect this e.g. to the standard input of another program.
The diagram below provides a breakdown of the format of output from ls -l
.
How to get help and documentation
Especially in the beginning you will have a lot of questions what a
command does and which arguments and parameters need to be given. One
rule before using a command or before asking somebody about it is
called RTFM (please check the
meaning yourself). Maybe the most important command is man
which
stands for manual. Most commands offer a manual and with man
you
can read those. To get the documentation of ls
type
$ man ls
To close the manual use q
. Search file content with /
followed by a search term/regular expression (more on regular expressions later). Additionally many tools offer some help via the parameter -h
,
-help
or --help
. For example ls
:
$ ls --help
Other tools will present this help if they are called without any parameters or arguments.
Bash keyboard shortcuts
There are different implementations of the Unix shell. You are currently working with Bash (Bourne-again shell). Bash has several keyboard shortcuts that improve the interaction. Here is a small selection: