19

I'm calling a shell script from another shell script and the called script requires some input (command line) parameters.
I'm have below mentioned code, but thats not working. I don't know why the argument values are not passed to the called script.

script1.sh
=======================================
#!/bin/bash
ARG1="val1"
ARG2="val2"
ARG3="val3"
. /home/admin/script2.sh "$ARG1" "$ARG2" "$ARG3"


script2.sh
=======================================
#!/bin/bash
echo "arg1 value is: $1 ....."
echo "arg2 value is: $2 ....."
echo "arg3 value is: $3 ....."

But when I execute script1.sh I get following result:

arg1 value is:  .....
arg2 value is:  .....
arg3 value is:  .....

What am I missing?

0

1 Answer 1

23

By sourcing the second script with . /home/admin/script2.sh, you're effectively including it in the first script, so you get the command line arguments to the original script in $@. If you really want to call the other script with arguments, then do

/home/admin/script2.sh "$ARG1" "$ARG2" "$ARG3"

(make sure it's executable).

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

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.