2

Suppose my parent script is called with n arguments, and I want to pass all arguments from argument #3 to the child script.

How do I do that in bash script?

I know $@ would get you the entire argument list, which is not what I want.

3 Answers 3

7

Like this:

child_script "${@:3}"

If you wanted to pass four arguments starting at argument 3:

child_script "${@:3:4}"
Sign up to request clarification or add additional context in comments.

Comments

4

Use shift to remove the first two arguments:

shift 2
child_script "$@"

If you need to use $1 and $2, you can save them in variables first.

Another option is to assign the arguments to an array:

args=("$@")

remove the first two elements of the array:

unset args[0]
unset args[1]

Then call the child script with this array:

child_script "${args[@]}"

2 Comments

But what if I want to use $1 and $2 later in the file? SHould I just save their values in a separate variable?
That's what I would probably do, but I added another solution to the answer.
0

As for the discussion of keeping the original values: You could do the shift in the child script.

Comments

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.