Software Carpentry logo

Basic Unix Shell

April 24, 2010: We are pleased to announce that Version 4 of this course is now under development. For updates and an early peek at the content, please check out the Software Carpentry blog at http://www.software-carpentry.org/blog/.

1) Introduction

A shell is a command line interpreter that is the interface between the user and the Operating System

2) You Can Skip This Lecture If...

3) A Shell is a Program That...

A Shell in Action

Figure 9.1: A Shell in Action

4) The Shell and the Operating System

The operating system is the low level software that runs everything

Operating System

Figure 9.2: Operating System

5) The File System

6) Paths

A path is a description of how to find something in a file system

7) Navigating the File System

$ pwd
/home/hpotter/swc
$ ls
LICENSE.txt     conf        data        docs    index.swc   license.swc     print.css   swc.css     tests
Makefile        config.mk   depend.mk   img     lec         press           sites       swc.dtd     util
$ ls data
bio     elements    haiku.txt   morse.txt   pdb         solarsystem.txt
$ cd data
$ pwd
/home/hpotter/swc/data
$ ls
bio     elements    haiku.txt   morse.txt   pdb         solarsystem.txt
$ cd ..
$ pwd
/home/hpotter/swc

8) Execution Cycle

9) Providing Options

$ ls -F
LICENSE.txt     conf/       data/       docs/   index.swc   license.swc     print.css   swc.css     tests/
Makefile        config.mk   depend.mk   img/    lec/        press/          sites/      swc.dtd     util/
$ ls -a
.   .svn        Makefile    config.mk   depend.mk   img         lec         press       sites   swc.dtd util
..  LICENSE.txt conf        data        docs        index.swc   license.swc print.css   swc.css tests
$ ls -a -F
.   .svn/       Makefile    config.mk   depend.mk   img/        lec/        press/      sites/  swc.dtd util/
..  LICENSE.txt conf/       data/       docs/       index.swc   license.swc print.css   swc.css tests/

10) Creating Files and Directories

$ mkdir tmp
$ cd tmp
$ ls
Name: Earth
Period: 365.26 days
Inclination: 0.00
Eccentricity: 0.02
Name: Venus
Period: 224.70 days
Inclination: 3.39
Eccentricity: 0.01
$ cp earth.txt venus.txt
$ edit venus.txt
$ ls -t
venus.txt   earth.txt

11) Looking at Files

$ cat venus.txt
Name: Venus
Period: 224.70 days
Inclination: 3.39
Eccentricity: 0.01
$ ls -l
total 2
-rwxr-xr-x  1 gvwilson None 73 Jan  4 15:58 earth.txt
-rwxr-xr-x  1 gvwilson None 73 Jan  4 15:58 venus.txt
$ wc earth.txt venus.txt
  4   9  73 earth.txt
  4   9  73 venus.txt
  8  18 146 total

12) Environment Variables

$ set
BASH=/usr/bin/bash
BASH_VERSION='2.05b.0(1)-release'
COLUMNS=120
HISTFILE=/home/.bash_history
HISTFILESIZE=500
HISTSIZE=500
HOME=/home/rweasley
HOSTNAME=hogwarts
HOSTTYPE=i686
LINES=60
NUMBER_OF_PROCESSORS=1
OSTYPE=cygwin
PATH='/usr/local/bin:/usr/bin:/bin:/Python24:/home/rweasley/bin'
PWD=/home/rweasley
SHELL=/bin/bash
UID=1003
USER=rweasley
$ echo $HOME
/cygdrive/c/home/rweasley

13) Commonly-Used Shell Variables

The default set of environment variables can be quite different on different machines and operating systems, but here are the most commonly used ones:

Name Typical Value Notes
EDITOR /bin/edit Preferred editor
HOME /home/rweasley The current user's home directory
HOMEDRIVE C: The current user's home drive (Windows only)
PATH "/home/rweasley/bin:/usr/local/bin:/usr/bin:/bin:/Python24/" Where to look for programs
PWD /home/rweasley/swc/lec Present working directory (sometimes CWD, for current working directory)
SHELL /bin/bash What shell is being run

Table 9.1: Shell Variables

14) Setting Environment Variables

$ VILLAIN="Lord Voldemort"

15) Scoping

$ VILLAIN="Lord Voldemort"
$ bash
$ echo $VILLAIN

  
$ exit
Setting a Variable Without Export It

Figure 9.6: Setting a Variable Without Exporting It

$ VILLAIN="Lord Voldemort"
$ export VILLAIN
$ bash
$ echo $VILLAIN
Lord Voldemort
$ exit
Exporting a Variable's Value

Figure 9.7: Exporting a Variable's Value

$ export VILLAIN="Lord Voldemort"
$ bash
$ echo $VILLAIN
- Lord Voldemort
$ exit

16) How the Shell Finds Programs

17) Common Search Path Entries

18) Basic Tools

man Documentation for commands.
cat Concatenate and display text files.
cd Change working directory.
clear Clear the screen.
cp Copy files and directories.
date Display the current date and time.
diff Show differences between two text files.
echo Print arguments.
head Display the first few lines of a file.
ls List files and directories.
mkdir Make directories.
more Page through a text file.
mv Move (rename) files and directories.
od Display the bytes in a file.
passwd Change your password.
pwd Print current working directory.
rm Remove files.
rmdir Remove directories.
sort Sort lines.
tail Display the last few lines of a file.
uniq Remove adjacent duplicate lines.
wc Count lines, words, and characters in a file.

Table 9.2: Basic Command-Line Tools

19) Summary