The Shell Scripting Tutorial

Interactive Shell

Here are a few quick hints for using the UNIX or Linux shell interactively. Personally I recommend the bash shell for most interactive use; it is available on just about every *nix flavour, and very pleasant to use as a login shell. However the root shell should always be /bin/sh, whether that points to bash or Bourne shell.


bash has some very handy history-searching tools; the up and down arrow keys will scroll through the history of previous commands. More usefully, Ctrl+r will do a reverse-search, matching any part of the command line. Hit ESC and the selected command will be pasted into the current shell for you to edit as required.

If you want to repeat a command you ran before, and you know what characters it started with, you can do this:

bash$ ls /tmp
(list of files in /tmp)
bash$ touch /tmp/foo
bash$ !l
ls /tmp
(list of files in /tmp, now including /tmp/foo)

As well as the arrow keys, you can use PageUp and PageDn to navigate to the start and end of the command line.


You can make ksh more usable by adding history commands, either in vi or emacs mode. There are a number of ways to do this, depending on the exact circumstances. set -o vi, ksh -o vi, or exec ksh -o vi (where "vi" could be replaced by "emacs" if you prefer emacs mode).

If you want to start a ksh session from another interactive shell, you can just call ksh like this:

csh% # oh no, it's csh!
csh% ksh
ksh$ # phew, that's better
ksh$ # do some stuff under ksh
ksh$ # then leave it back at the csh prompt:
ksh$ exit

This will start a new ksh session, which you can exit from and return to the previous shell. Alternatively, you could replace the csh (or whatever shell) with a ksh shell, with the exec command:

csh% # oh no, it's csh!
csh% exec ksh
ksh$ # do some stuff under ksh
ksh$ exit


The difference here is that you don't get the csh session back.

The good stuff is the history:

csh% ksh
ksh$ set -o vi
ksh$ # You can now edit the history with vi-like commands, 
  # and use ESC-k to access the history.

If you hit ESC then k, then by repeatedly hitting k you scroll backwards through the command history. You can use vi command-mode and entry-mode commands to edit the commands, like this:

ksh$ touch foo
  ESC-k (enter vi mode, brings up the previous command)
  w (skip to the next word, to go from "touch" to "foo"
  cw (change word) bar (change "foo" to "bar")
ksh$ touch bar

  Previous: Quick Reference  Next: Exercises   

My Paperbacks and eBooks

My Shell Scripting books, available in Paperback and eBook formats. This tutorial is more of a general introduction to Shell Scripting, the longer Shell Scripting: Expert Recipes for Linux, Bash and more book covers every aspect of Bash in detail.

Shell Scripting Tutorial

Shell Scripting Tutorial
is this tutorial, in 88-page Paperback and eBook formats. Convenient to read on the go, and in paperback format good to keep by your desk as an ever-present companion.

Also available in PDF form from Gumroad:Get this tutorial as a PDF
Shell Scripting: Expert Recipes for Linux, Bash and more

Shell Scripting: Expert Recipes for Linux, Bash and more
is my 564-page book on Shell Scripting. The first half covers all of the features of the shell in every detail; the second half has real-world shell scripts, organised by topic, along with detailed discussion of each script.