Updated: 2012-09-10 10:15 EDT

1 Syntax of Control Characters - ^A ^B ^C etc. Index up to index

The syntax ^C stands for the single ASCII character CONTROL-C often written as CTRL-C or perhaps [CTRL-C]. To generate this character from your keyboard, hold down the CTRL key and simultaneously push the letter C key (upper or lower case; doesn’t matter). This syntax and method applies to all the control characters, e.g. ^A, ^B, ^C, ^D, etc.

2 What is a shell for? Index up to index

To find and run programs.
(“Programs” are also called commands or utilities.) A shell is itself a program that finds and runs other programs.

Shells also do programming kinds of things, but that programming is usually to aid in the finding and running of programs, not to do any kind of substantial mathematical or business calculations. (Shells do not do mathematics very well.)

Part of running a program involves supplying command line arguments to that program; the shell helps with that, too.

Command names typed into shells almost always resolve to be the names of executable files stored in system directories. (Some command names are not files; they are built-in to the shell, such as cd.)

For example, the shell may resolve the command name ls to run the executable file /bin/ls, passing it any arguments you type on the command line. You can control where the shell looks for command executables by setting the $PATH variable.

3 What is a “Bourne” shell? What is a “C” shell? Index up to index

The shells sh, ksh, zsh, and bash (the “Bourne” shells) all have a common ancestry. They are all derived from the original shell “sh” written in the 1970’s by Stephen Bourne. The programming features of these shells (if statements, for loops, etc.) all look and work the same way. This is the best shell to study.

The shells csh and tcsh (the “C” shells) are similar, having a history dating back to Bill Joy at Berkeley in the 1980’s. Their syntax for programming is not the same as the Bourne shells. We do not cover the C shell syntax in this course; these shells are notoriously buggy.

C Shell Bugs: Google for:   c shell bugs

My list of C Shell Bugs: Google for:   c shell bugs idallen

4 How does the shell help find and run commands? Index up to index

5 Using a Shell inside a Terminal Index up to index

Shells run inside “terminals”, usually software terminal windows on modern GUI systems. These terminals recognize some useful command sequences that aid in typing commands. Below are some common command sequences:

   CTRL-C - *interrupt* a running foreground program
   CTRL-D - send *End-of-File* (EOF) from your keyboard (end input)
   CTRL-Z - temporarily *suspend* a process; restart by typing "fg"
   CTRL-U - erase input characters back to the start of the prompt
   CTRL-W - erase just the last word you typed (repeat for next word)

The above commands are properties of the terminal, so they work for any program that reads your keyboard, not just shells. You can change any of the above mappings using the stty command.

Note that suspending a text editor (such as vim) is a very dangerous thing to do, since having multiple editors all editing the same file can cause data loss if you write out an old version of a file on top of a newer one. Be very careful about suspending processes. Suspending is not the same as killing or terminating!

6 Basic Command Line Syntax Index up to index

Most Unix commands need both a VERB (what to do - the command name) and an OBJECT (what to do it on - a command argument):

   $ cat /etc/passwd               (command argument is a file name)
   $ mail idallen@idallen.ca       (command argument is an email address)
   $ kill 1234                     (command argument is a process number)

The following incorrect attempts at Unix commands are wrong:

   $ /etc/passwd         (missing VERB; what are you trying to DO?)
   $ cat                 (missing OBJECT; catenate WHAT file?)

Remember to tell Unix both what you want to do and to what object you wish to do it:

   $ cat /etc/passwd
   $ less /etc/passwd
   $ wc /etc/passwd
   $ sort /etc/passwd

7 Most (but not all) commands take what as arguments? Index up to index

Most - but not all - Unix commands take pathnames as arguments. Much of what people do on their computers is manipulate files and directories. This is why shells have GLOB pattern matching characters.

“Pathnames” are names that might be file names or directory names. (Unix also has names that are neither files nor directories, e.g. the /dev/null pathname is a “character special” device.)

The shell has GLOB (wildcard) features to make matching pathnames easier. e.g. try: $ echo /bin/*

8 Getting Out of Programs - exit, quit, etc. Index up to index

Different Unix programs have different ways of ending or exiting from them. If you’re stuck at a prompt and can’t get out, try various things such as these:

h or help or ?

q or Q or quit
:q!  (used in VI to exit without saving)
x or exit
^D   (CTRL-D)
^C   (CTRL-C)
logout
bye
ESC  (the ESC key)
.    (a period)
^\   (CTRL-backslash)

One of the above usually works. Sometimes you can use ^Z (CTRL-Z) to “stop” the process temporarily, and then type “kill %%” to terminate it. (Remember to terminate it, or it will sit there waiting forever. A process stopped with ^Z still has all its files open.)

9 Interrupting Processes and signalling End-of-File - ^C and ^D Index up to index

The terminal emulator treats the characters ^C and ^D specially.

9.1 Interrupting a Process - ^C Index up to index

If you type a command and decide you want to interrupt (terminate) it, the terminal emulator lets you do this with a control character.

To send an interrupt signal to a process that is running on your terminal, use the Interrupt Character, usually CTRL-C (^C). (You can program a different character; sometimes DEL is used.) The Interrupt usually throws away any pending input/output for the process and causes the process to terminate abnormally. Use it with caution.

Interrupting a process usually terminates the process. Whatever the process was doing is left incomplete and unfinished. Buffers are not flushed; files will be incomplete. An interrupted process did not finish.

9.2 Signalling End-of-File (EOF) from the keyboard - ^D Index up to index

When a program reads a file, it naturally stops reading when the end of the file is reached - this is called End-of-File or EOF. When the program is reading from your keyboard, it doesn’t know when to stop reading unless you tell it by typing the EOF character into the terminal.

Your EOF character, signalling EOF from your keyboard, is usually CTRL-D (^D). You must type this at the beginning of a line (or type it twice). (You can program a different character; but, it is almost never done.)

Typing the EOF character tells the process reading your keyboard that you are finished typing at your terminal (EOF); but, it does not interrupt or terminate the process. The process will finish reading your keyboard and the continue with whatever it is doing. Often, that means producing some output and then exiting successfully.

(Note: Unlike DOS/Windows systems, ^D does not actually send any character to the process reading your keyboard - it simply tells the process that EOF has happened. You cannot put ^D inside a file and have it serve as an EOF. The ^D only works when typed from a keyboard inside a terminal.)

9.3 Examples of ^C and ^D Index up to index

Here is an example showing how ^C interrupts a program before it finishes, but ^D signals an EOF and lets the program finish:

9.3.1 ^C Interrupting a Process (does not finish) Index up to index

$ sort >out
enter some lines of text and then
interrupt the program
and you will get an empty file
^C
$ cat out
$ 

9.3.2 ^D Signalling EOF (end-of-file) lets process finish Index up to index

$ sort >out
enter some lines of text and then
use an EOF to signal end of file
and sort will sort your data and finish
^D
$ cat out
and sort will sort your data and finish
enter some lines of text and then
use an EOF to signal end of file
$

Typing ^C interrupts a process, which usually terminates it. Typing ^D signals EOF from your keyboard to the process.

10 Nested Shells inside Shells Index up to index

Your default login shell on Linux is the Bourne-Again shell (bash). You can call up other shells by name if they are installed: sh, ksh, bash, csh, tcsh, etc. Not all shells may be installed on your system.

You can call up a shell inside a shell (a “nested” shell) by typing its name at the shell prompt (e.g. bash). The new copy of the shell will take over reading your keyboard until you exit from it again and the previous shell returns to read your keyboard.

To exit from a shell type: exit

When you exit from the first shell in a terminal, either the terminal window closes (if using a terminal window), or, if you are logged in directly to a Unix/Linux console, you log out from Unix.

Another way to exit a shell is to type your EOF character. (Your EOF character is usualy CONTROL-D.) Some shells can be told to ignore EOF.

Remember: all Unix programs (should) have manual pages!

$ man sh
$ man ksh
$ man bash
$ man csh
$ man tcsh

Each new shell is another Unix process. To get a list of your current processes in the current terminal, use: ps

11 Shell interactive pathname completion Index up to index

If you type part of a pathname on a comand line and then push the TAB key (once or twice), many shells will attempt to complete the pathname for you. If the pathname cannot be completed unambiguously, the shell will show you a list of possible completions:

   bash$ echo /etc/pas<TAB>
   passwd      passwd-     passwd.OLD  

If you type part of a command name and push TAB, the shell will list all the possible commands in your PATH that start with those letters:

   bash$ mkd<TAB>
   mkdep      mkdict     mkdir      mkdirhier  mkdosfs    

Use the shell to do your typing for you - learn how to use the TAB key! (The shell always spells your pathnames correctly!)

12 Shell “history” of previous commands Index up to index

Most shells keep a record (“history”) of the commands you type. The history is often saved in a file when the shell exits, and is restored when you next log in. You can see the history list using the built-in “history” command.

Many shells allow you to use the UP-ARROW and DOWN-ARROW keys to move up and down in the command history. (Some shells use other key sequences, such as ^P and ^N.) Move up to the command you want to re-execute, modify it as needed, then use [Enter] to execute it.

Some shells let you select items from the history list by number (e.g. “!123”). See History in your shell’s man page.

Each shell has its own separate command history. Starting a new shell will start an independent history any may not pick up the history of another shell unless that other shell saved it and your new shell loads it.

13 Shell command line alias trouble Index up to index

Watch out for “helpful” system administrators that define aliases for your shells when you log in. (This is true on most versions of Unix/Linux.) The aliases may mislead you about how Unix commands actually work.

For example, the rm command does not prompt you for confirmation when removing a file. On some systems, when you log in, rm has been made to be an alias for rm -i, which does prompt. If you become accustomed to the behaviour of the alias, you will be in serious trouble when you move to a system that does not define the alias.

To avoid pre-defined aliases, sometimes you can start up a fresh copy of the shell that has no aliases defined:

   $ alias
   [...many aliases may print here...]

   $ bash
   bash$ alias
   [...no more aliases here...]

The other thing you can do is execute unalias -a to remove all your aliases for the current shell. You can put this into your shell start-up file (e.g. .bashrc) to do it every time you start a new shell.

To define your own aliases, look up “aliases” in a Unix/Linux text index, e.g.:

   $ alias dir='ls -lF'
   $ alias cd..='cd ..'
   $ alias mali=mail

Aliases defined in the current shell are not saved when the shell exits. You must put your own alias definitions in a shell start-up file to have them restored between sessions (e.g. put them into your .bashrc file).

Each shell has its own separate aliases. Starting a new shell does not get the aliases defined in other shells.

Author: 
| Ian! D. Allen  -  idallen@idallen.ca  -  Ottawa, Ontario, Canada
| Home Page: http://idallen.com/   Contact Improv: http://contactimprov.ca/
| College professor (Free/Libre GNU+Linux) at: http://teaching.idallen.com/
| Defend digital freedom:  http://eff.org/  and have fun:  http://fools.ca/

Plain Text - plain text version of this page in Pandoc Markdown format


Campaign for non-browser-specific HTML   Valid XHTML 1.0 Transitional   Valid CSS!   Creative Commons by nc sa 3.0   Hacker Ideals Emblem   Author Ian! D. Allen