A brief tutorial on TTYs, BASH command line prompts, etc.



The command line suggestions given here work in bash and haven't yet been tested on other shells. Most certainly, the prompt string is based on bash, and won't work on zsh, tcsh, ash, or other Linux shells.

A tty is an abbreviation for a manufacturer of Unix terminals in the old days called Teletype. The definition in LINUX has expanded so it can refer to many things, and right now I am using the term when referring to the devices on your LINUX system that treat your computer like the serial-line, ASCII character-based terminals of the olden (pre-1984!) days. Think of plain, vanilla MS-DOS when I write of tty terminals. Before firing up X, and logging on, that is the kind of terminal you are working on. Redirecting tail to one of these serial-type lines has the effect of redirecting it to one of these ttys.

In order to view tail output on a tty that is does not have getty running on it, you must know which tty you are logged on to right now so that you can return to it. To see this, issue the command tty, and it will tell you. You can also put the tty number in your prompt for a permanent record. Here are two lines to put in your .bashrc:

        SCRN=`tty`  # this works better because of the parsing below
        PS1='Screen number ${SCRN#/dev/tty} > '
This will give a prompt that looks something like:
        Screen number 1 >
This will work in the old Unix Korn shell (ksh) and bash. It may work in zsh, but not many other shells.

If you want to get really fancy, you can do this in your .bashrc file:

        SCRN=`tty`
        case $TERM in
             xterm*) # puts a nice updatable label in the titlebar
                     PS1='\[\033]0;(\u) Screen ${SCRN#/dev/ttyp} [\w]\007\]\s (\!) \$ '
                     PS2=' > '
             ;;
             *) # tty terminals still get an informative prompt
                     PS1='(${USER%${USER#????}}) Scr ${SCRN#/dev/tty} [\w] (\!) \$ '
                     PS2=' > '
             ;;
        esac

And the general effect for username "Stewart" is (on a tty in the home directory):

        (Stew) Scr 1 [~] (1) $

And, in an xterm shell, you'll see:

        bash (1) $

... with this appearing in the xterm titlebar:

        (Stew) Screen 1 [~]

... meaning that each xterm will be numbered. With this being done, you ought to be able to know which tty you are using at a glance.

Another reason why this prompt style is useful: multiple logins

LEFT ALT+Fn, where n is the number of a function key between 1 and 10 on an IBM 101-key keyboard leads you to any of a number of login prompts. n also corresponds to the particular tty device as numbered in the /dev directory (for example, access /dev/tty4 by pressing LEFT ALT+F4). Depending on your LINUX distribution, you can have as many as 6 possible shells operating at once. You can telnet one one, edit on another, use pine on another, and so on. This is made possible through minigetty, which gives you the Login: prompt on each screen that is available. You can have 24 such gettys running at once on a standard PC with 4 megs of RAM and 12 megs of swap (I've tried it) by editing /etc/inittab. See Running Linux, by Welsh and Kaufman (pages 124-125) for more details.

In using multiple shells, the prompt as shown above tells you which directory that shell is in, as well as which tty you are using. This is great when you are moving from screen to screen, when it is easy to lose track of which directory you are in and which screen you are using.

Back to the the "PPP Dialin Scripts" page

Back to the home page

since Apr 1 2007