Home‎ > ‎CIS 98‎ > ‎

Shell Basics

Lesson 2: Shell Basics 

In this lesson we review the fundamental operations of the shell. 


  • Describe six features (responsibilities) of the shell. 
  • Describe how the shell parses the command line. 
  • Describe how the shell locates and executes commands. 
  • Identify three UNIX command types. 

Key Commands

The following commands are key to operating the shell. They will be used extensively in this course: 
  • date
  • who
  • echo
  • ls
  • cat
  • wc
  • cp
  • mv
  • rm
  • pwd
  • cd
  • mkdir
  • rmdir
  • sort
If some of the command don't look familiar try reading the manual page with the man command. For example, if you want to know how the sort command works:

$ man sort 

To learn more about how to read manual pages read this article from my CIS-191 class:

    Deciphering Manual Pages

Using Pipes

Pipes are a feature of the shell that enables the output of one command to be passed to the input of another command. Here's an example of a command that counts the number of processors on a system using pipes: 

$ grep processor /proc/cpuinfo | wc -l 

Without the pipe the grep command does this: 

$ grep processor /proc/cpuinfo
processor : 0
processor : 1
processor : 2
processor : 3
processor : 4
processor : 5
processor : 6
processor : 7

Piping that output to wc -l counts the lines out output. 

I/O Redirection

The shell can redirect the output of a command to a file. Each process has two outputs:
  1. STDOUT - The normal output of the command 
  2. STDERR - Error messages from the command
Redirecting output and error is done with the '>' operator. For example. The following command lists all files on the system and saves them to the file all-files.txt:

$ find / > all-files.txt 

Run the command and you'll notice that there's still output. The output is from errors where you do not have the permission to read a directory. You save those errors into the same file like this:

$ find / > all-files.txt 2>&1 

The '2>&1' says redirect STDERR to the same thing that STDOUT is redirected to. This DOES NOT WORK:

# ERROR - opens the file twice
$ find / > all-files.txt 2> all-files.txt 

You can discard the errors by redirecting them to /dev/null 

$ find / > all-files.txt 2>/dev/null 

UNIX Architecture
    1. The Kernel
      The kernel is a single large executable program that is loaded in memory as part of the boot process and remains in memory servicing applications and managing resources as long as the computer is running.
      1. System call Interface
      2. Process control
      3. Resource Management
      4. Device Drivers
    2. The Shell
      Your shell is the program executed in your behalf by the login program when you log in. This program is specified in the file/etc/passwd.
      1. Provides a user interface through which users interact with the system
      2. Prompts users for commands to be executed and then executes them for the user
      3. Provides a configurable environment allowing users to modify commands, prompts, and other system behaviors

  1. Shell Features
    1. Command-line Interpreter
    2. Filename Expansion
    3. I/O Redirection 
      1. File descriptors
      2. Input redirection
      3. Output redirection
      4. Pipelines
    4. Environment Control
    5. Programming Language

  2. Parsing the Commandline
    1. Processing Order
      1. First pass: identify shell metacharacters
      2. Second pass: do variable and command substitution
      3. Third pass: perform filename expansion
      4. Fourth pass: I/O redirection
      5. Determine execution type
    2. Execution Types
      1. Foreground, (sequential)
      2. Background
      3. Sourcing
      4. Conditional execution

  3. Command Types
    1. Binary Programs
      Programs created by a compiler or assembler. These are the most common type of command; they execute very quickly.
      1. cat
      2. vi
      3. ps
      4. bash
    2. Shell Built-in Commands
      These "commands" are built into the shell binary itself; their functionality is determined by the shell.
      1. cd
      2. exit
      3. type
    3. Shell Scripts
      Also known as shell programs, command files, and batch files, are text files that contain a list of commands which are executed by the shell.
      1. spell
      2. ldd
      3. apropos

Exercise 1

Given a directory with these files:
	report		report_feb	report_jun
	report_apr	report_jan	reports
What is the output of each of these command lines?
echo *
echo report_*
echo report?
echo re*[nl]
echo r*x

Exercise 2

For each of these command lines, indicate which file or file descriptor the sort command (not the shell) is opening for input and output:
sort data1
sort data2 -o data3
sort < data4
sort > data5
sort < data6 > data7
sort < data8 -o data9
sort data10 < data11 > data12
sort data13 > data13
sort < data14 > data14

Exercise 3

In the command line:
cat > datafile
The cat command opens the stdin (presumably the keyboard), and writes to the stdout, (which the shell redirected to datafile). In this example, the stdin terminates when the user enters ctrl-d from the keyboard.

Modify the command line so that the stdin terminates when the user enters a period (.) on a line by itself.