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


UNIX Power Tools

UNIX Power ToolsSearch this book
Previous: 10.8 Fix Quoting in csh Aliases with makealias and quote Chapter 10
Aliases
Next: 10.10 Simulated Bourne Shell Functions and Aliases
 

10.9 Shell Functions

sh_init
The C shell has aliases (10.2 ) . But until System V Release 2, the Bourne Shell had almost (10.10 ) no way for users to set up their own built-in commands. Functions are like aliases, but better. For instance, functions can return a status (44.7 ) and have much more reasonable syntax (10.7 ) . bash and the Korn Shell have shell functions, too. To find out all about functions, check a shell programming book. There are examples in the sh_init file on the CD-ROM. Here are the examples from articles 10.2 and 10.3 changed into Bourne shell aliases:
  • The la function includes "hidden" files in ls listings. The lf function labels the names as directories, executable files, and so on:

    la () { ls -a "$@"; }
    lf () { ls -F "$@"; }

    The spaces and the semicolon (; ) are both important! [3] The "$@" (44.15 ) is replaced by the command-line arguments (other options, or directory and filenames), if you use any:

    [3] A function is a Bourne shell list construct (13.8 ) . You can omit the semicolon in bash -but, if you do, your functions won't be portable.

    $ la -l somedir
    
                ...runs 
    ls -a -l somedir

  • This next simple function, cur , gives the name of your current directory and then lists it:

    cur()
    {
        pwd
        ls
    }

That example shows how to write a function with more than one line. In that style, with the ending curly brace on its own line, you don't need a semicolon after the last command.

- JP


Previous: 10.8 Fix Quoting in csh Aliases with makealias and quote UNIX Power Tools Next: 10.10 Simulated Bourne Shell Functions and Aliases
10.8 Fix Quoting in csh Aliases with makealias and quote Book Index 10.10 Simulated Bourne Shell Functions and Aliases

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