next up previous 405
Next: Predefined variables
Up: Shell Variables
Previous: Prompting


Script arguments and other special variables

The C-shell has some special forms of variable. We have seen some already: the $< for prompting, and how to specify array elements. The remainder largely concern command-line arguments. Here they are tabulated.

Syntax Meaning
   
${0} The name of the script being run
$?name Returns 1 if the variable name is defined, or 0 if it is not defined
$n The value of the n$^{\rm th}$ argument passed to the script
$argv[n] The value of the n$^{\rm th}$ argument passed to the script
$#argv The number of arguments passed to the script
$* All the arguments supplied to the script
$$ Process identification number (useful for making temporary files with unique names)
   
Thus inside a script, any command-line arguments are accessed as shell variables $1, $2 ...$#argv. There is no practical limit to the number of arguments you can supply.

Let's look at a few examples. Below is a script argex.csh.

     #! /bin/csh
     echo ${0}
     echo "Number of arguments is $#argv"
     echo $2
     echo $argv[2-3]
     echo $argv[$]
     exit
Then we run it with four arguments.

     % argex.csh "hello world" 42 3.14159 "(300:400,~100)"
     argex.csh
     Number of arguments is 4
     42
     42 3.14159
     (300:400,~100)
Note that you must enclose any argument passed to a script that contains a space or shell metacharacter in double quotes (").

You can see an example using $* to process a list of files here. There are other examples of script arguments in UNIX options.


next up previous 405
Next: Predefined variables
Up: Shell Variables
Previous: Prompting

C-shell Cookbook
Starlink Cookbook 4
Malcolm J. Currie
2006 November 26
E-mail:ussc@star.rl.ac.uk

Copyright © 2009 Science and Technology Facilities Council