2

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?

1

1 Answer 1

4

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.

Sign up to request clarification or add additional context in comments.

1 Comment

shift can take an argument, too, if you need to drop more than one at once: shift 2, for example.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.