1

Is it possible to modify the arguments passed to a bash program ? and then pass them to a Java program ?

I know we can access all the arguments passed to a bash program by "$@" and I can pass them to Java program like java com.myserver.Program "$@". But is it possible to modify value of certain arguments inside "$@" and then call the above java program with "$@" ?

I also know you can use "$@[1]" to access the value of arguments but how can we iterate over them and change the value at the proper position ? I also know this :

for arg
do
....
done

But inside the do loop how can the value of argument be modified and then java program be called ?

1 Answer 1

4

Iterate through every item in the positional parameters, then add them to another array, modifying it if needed while you do.

ARGS=()
for A in "$@"; do
    # Modify A then add it to args. 
    # A=${A//something/something} ## Just an example.
    ARGS+=("$A")
done

# Then call java:

java com.myserver.Program "${ARGS[@]}"

And since you're processing positional parameters, it would be simpler to use this form of for:

for A; do
Sign up to request clarification or add additional context in comments.

3 Comments

I was thinking of doing this : stackoverflow.com/a/3156914/1291961. But how can I pass that as argument to Java class?
@WarriorPrince On my example it's java com.myserver.Program "${ARGS[@]}".
Yes Just saw the edit. Awesome it worked. Thank you very much for detailed explanation.

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.