tags:

views:

56

answers:

2

If I pass any number of arguments to a shell script that invokes a Java program internally, how can I pass second argument onwards to the Java program except the first?

./my_script.sh a b c d ....

#my_script.sh
...
java MyApp b c d ...

Thanks,

+8  A: 

First use shift to "consume" the first argument, then pass "$@", i.e., the list of remaining arguments:

#my_script.sh
...
shift
java MyApp "$@"
Bolo
The special parameter `@` should "always" be quoted: `"$@"`, otherwise is not different from `$*`. Also, should be mentioned that after the `shift`, if not previously saved, the first parameter is lost.
enzotib
@enzotib Thanks, I've quoted `$@`.
Bolo
+1  A: 

You can pass second argument onwards without using "shift" as well.

set -- 1 2 3 4 5

echo "${@:0}"
echo "${@:1}"
echo "${@:2}"   # here
bashfu
does not work in `sh`, only `bash`. this is called substring expansion and has a special behaviour for `@`. usually it counts the characters, but for `@` it counts the parameters.
lesmana