home | O'Reilly's CD bookshelfs | FreeBSD | Linux | Cisco | Cisco Exam  


UNIX Power Tools

UNIX Power ToolsSearch this book
Previous: 7.3 C Shell Prompt Causes Problems in vi, rsh, etc. Chapter 7
Setting Your Shell Prompt
Next: 7.5 Multiline Shell Prompts
 

7.4 Faster Prompt Setting with Built-Ins

To set your prompt, you execute a command (on most shells, the command sets a shell variable). Before setting the prompt, you may run other commands to get information for it: the current directory name, for example. A shell can run two kinds of commands: built-in and external (1.10 ) . Built-in commands usually run faster than external commands. On a slow computer, the difference may be important - waiting a few seconds for your prompt to reset can get irritating. Creative use of your shell's built-in commands might pay off there. Let's look at some examples:

  1. pwd is an external command that searches the filesystem (14.4 ) to find your current directory name. (pwd is built into some shells, but that version doesn't search the filesystem.) However, some shells can give you the current directory name from a variable, usually $cwd or $PWD . On slow computers, the first prompt-setting command below would take more time:

    `...`
     
    set prompt="`pwd`% "
    set prompt="${cwd}% "

    There's a tradeoff here, though - the shell built-in may not (14.13 ) give the right answer. Also, in the C shell, each time you change to a new directory, you need to run a new set prompt command; you can use an alias like setprompt (7.5 ) to do this automatically.

  2. If you're putting your current directory in your prompt, you may only want the tail of the pathname (the name past the last slash). How can you edit a pathname? Most people think of basename (45.18 ) or sed (34.24 ) . Using the current directory from $cwd , they might type:

    set prompt="`basename $cwd`% "

    The faster way is with the C shell's built-in "tail" operator, :t :

    {}
     
    set prompt="${cwd:t}% "

    If your current directory is /usr/users/hanna/projects , either of those prompts would look like this (with a space after the percent sign):

    projects%

    The C shell has several of these built-in string operators (9.6 ) like :t ; the Korn Shell and bash have more-powerful string operators (9.7 ) .

  3. The Korn shell and bash can include the current value of another shell variable in their prompt. So, put $PWD (6.3 ) in your prompt string (the PS1 shell variable) and the prompt will always show the current directory. Or use any other variable; anytime it changes, the prompt will change too. The important trick is to store the prompt with single quotes (' ), not double quotes (" ), so that the variable name in your prompt won't be evaluated (8.14 , 8.5 ) until the prompt is actually printed to the screen.

    For example, I'll put the current directory and the value of a variable named PSX in my prompt. When I change either, the prompt changes too:

    $ PSX=foo
    
    
    $ PS1='$PWD $PSX\$ '
    
    
    /home/jerry foo$ PSX=bar
    
    
    /home/jerry bar$ cd ..
    
    
    /home bar$

  4. tcsh and bash also have special prompt string customizations that let you include the hostname, username, time, and more. You don't need external UNIX commands; you don't need to use an alias like setprompt to reset your prompt string.

    For example, to make your shell prompt show your current directory, a newline character (to move to the next line of a two-line prompt (7.5 ) ), then the current time, and finally a $ or % :

    PS1='$PWD\n\t \$ '            ...bash
    
    set prompt = '%~\\            ...tcsh
    
    %p%% '

    For more information, see O'Reilly & Associates' Using csh & tcsh and Learning the bash Shell -or your shell's manpage.

So, if your prompt takes too long to set, look for built-ins that can save time. As another example, article 7.11 shows how to use dirs in a shell prompt.

- JP


Previous: 7.3 C Shell Prompt Causes Problems in vi, rsh, etc. UNIX Power Tools Next: 7.5 Multiline Shell Prompts
7.3 C Shell Prompt Causes Problems in vi, rsh, etc. Book Index 7.5 Multiline Shell Prompts

The UNIX CD Bookshelf NavigationThe UNIX CD BookshelfUNIX Power ToolsUNIX in a NutshellLearning the vi Editorsed & awkLearning the Korn ShellLearning the UNIX Operating System