Share on Twitter Share on LinkedIn Share on StumbleUpon

16. 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

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.

ksh

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
csh%

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

login:

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
Share on Twitter Share on LinkedIn Share on StumbleUpon
Back: Quick Reference Next: Exercises

 

 


Books and eBooks

Contact

You can mail me with this form. If you expect a reply, please ensure that the address you specify is valid. Don't forget to include the simple addition question at the end of the form, to prove that you are a real person!