Shell Functions
sh_init | The C shell has aliases (). But until System V Release 2, the Bourne Shell had almost () 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 () and have much more reasonable syntax (). bash and the Korn Shell have shell functions, too. To find out all about functions, check a shell developing tutorial. There are examples in the sh_init file on the tutorial. Here are the examples from articles and 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"$@"
() 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 (). 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