tags:

views:

54

answers:

1

I make a clj script for running clojure as follows.

java -cp $CLOJURE_JAR:$CLASSPATH clojure.main $1

The problem is that $1 is the name of the script, so I can't pass the argument with this.

The alternatives can be

java -cp $CLOJURE_JAR:$CLASSPATH clojure.main $1 $2 $3 $4 $5

hoping that the number of arguments is less than four, which might work, but I guess there should be a better solution to this.

What would be the better way than this?

+3  A: 

You can use $@ to pass on all CLI arguments received by your script:

#!/bin/sh
java -cp $CLOJURE_JAR:$CLASSPATH clojure.main $@

If you want to omit some initial arguments, you can use e.g. shift, which drops the current value of $1 and shifts the remaining arguments so that $1 assumes the old value of $2, $2 that of $3 etc.:

#!/bin/sh
# prints out the first CLI argument, then passes the rest on to clojure.main
echo $1
shift
java -cp $CLOJURE_JAR:$CLASSPATH clojure.main $@

For related information, see the section entitled Special Parameters in bash's manpage.

Michał Marczyk
`shift` can take an argument, too, if you need to drop more than one at once: `shift 2`, for example.
Dennis Williamson