DOC HOME SITE MAP MAN PAGES GNU INFO SEARCH PRINT BOOK
 
Programming with the UNIX system shell

Special parameters


``$#''
This parameter, when referenced in a shell program, contains the number of arguments with which the shell program was invoked. Its value can be used anywhere in the shell program.
Enter the command line, shown in the following screen, in the executable shell program called get.num. Then run the cat command on the file:
   $ cat get.num
   echo The number of arguments is: $#
   $
The program simply displays the number of arguments with which it is invoked. For example:
   $ get.num test out this program
   The number of arguments is: 4
   $
You can write a simple shell program to demonstrate ``$*''. Create a shell program called show.param that will echo all the parameters. Use the command line shown in the following completed file:
   $ cat show.param
   echo The parameters for this command are: $*
   $
The program show.param will echo all the arguments you give the command. Make show.param executable and try it using these parameters:
   Hello.  How are you?
   $ show.param Hello.  How are you?
   The parameters for this command are: Hello.  How are you?
   $
Notice that show.param echoes Hello. How are you? Now try show.param using more than nine arguments:
   $ show.param a b c d e f g h i j
   The parameters for this command are: a b c d e f g h i j
   $
Once again, show.param echoes all the arguments you give. The ``$*'' parameter can be useful if you use filename expansion to specify arguments to the shell command.

Use the filename expansion feature with your show.param command. For example, suppose you have three files in your directory named for the first three chapters of a book. The show.param command prints a list of all those files.

   $ show.param chap?
   The parameters for this command are: chap1 chap2 chap3
   $

Next topic: Named variables
Previous topic: Positional parameters

© 2004 The SCO Group, Inc. All rights reserved.
UnixWare 7 Release 7.1.4 - 27 April 2004