Search code examples
unixparametersshelltcsh

Passing partial arguments in tcshell


I'm writing a shell script (tcsh) that is supposed to received 3 parameters or more. The first 3 are to be passed to a program, and the rest are supposed to be passed to another program. All in all the script should look something like:

./first_program $1 $2 $3
./second program [fourth or more]

The problem is that I don't know how to do the latter - pass all parameters that are after the third.


Solution

  • You can make use of shift as follows:

    ./first_program $1 $2 $3
    
    shift # shift 3 times to remove the first 3 parameters.
    shift
    shift
    
    ./second program $* 
    

    $* will contain the remaining parameters.

    You must also do error checking before you do a shift by checking $#argv and ensuring it is non-zero. Alternatively you can check the value of $#argv at the star of the script and ensure that it is atleast 3.