1

How do I pass the entire argument list of shell script to a program?

So for instance, if I have a java program called JavaFoo that'd take any number of arguments, and this should be called inside my script.

How do I pass the script's arguments to the java program?

0

1 Answer 1

1

In Bash:

#!/usr/bin/env bash
java /path/to/JavaFoo "$@"

to pass all parameters individually quoted as passed to the Bash script itself. Works with Dash as well, but I don't know about other Bourne-compatible shells.

Say you want to pass all but the first parameter to the shell script on to the Java program, use shift like this:

#!/usr/bin/env bash
shift
java /path/to/JavaFoo "$@"

Use more instances of shift to get rid of more parameters before passing them on, if needed.

So say you call the shell script (myscript) as follows:

./myscript a b c

the shift will get rid of a. Add one more shift and it also gets rid of b. The name of the script itself is not included in "$@" even though you can access it as $0 in many shells.

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

4 Comments

Yeah, adding the this works for me: java JavaFoo "$@". Thanks
Oops, sorry. I was assuming the Java file had a hashbang :)
What would the shift do in this particular case? So say, if I called the script with the following command ./myscript a b c, then script would get rid of the 0 argument (which is `myscript') and only pass 'a b c' to my java program?
Makes perfect sense now. Thanks

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.