1

This is my first time using a shell script.

I have one "Mother" script mainscript.sh, in which I define the variable patientid.

patientid=`basename $folder`

Later on in the same script, I wish to execute a separate script example.sh while passing the variable patientid into it. That script already has the variable labeled as "$patientid" in it. Looking at mainscript.sh below:

./example.sh #I WANT TO PASS THE VARIABLE patientid INTO HERE!

I know this is easy-peasy for y'all. Any help is appreciated! Thanks.

1
  • Does example.sh only refer to $patientid or does it set it somewhere, like in patientid=somevalue? Commented Jan 27, 2015 at 21:27

3 Answers 3

1

Before calling example.sh, mark patientid for export to the environment of child processes (such as the shell that will run example.sh):

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

Comments

1

You can run any shell script (in fact, even binary programs) with a set of variables predefined with

 VAR1=value1 VAR2=value2 ... script.sh

e.g.

 patientid=$patientid mainscript.sh

This assumes a Bourne-heritage shell (sh, bash, ash, ksh, zsh, ...)

Comments

0

Perhaps, what you need is to pass the parameter through the command line arguments, i.e.:

./example.sh $patientid

in the main script and

patientid=$1

in the example.sh script.

2 Comments

so, having patientid=$1 will automatically define it in example.sh the way it was defined in mainscript.sh? Just by adding $patientid after the execution command?
Right. ./example.sh $patientid will execute ./example.sh with $patientid as the command line argument. On the other side, the script example.sh will be able to access that argument as $1 (which is a standard name for the first command line argument) and assign it to patientid by doing patientid=$1. Obviously, this command will overwrite the old content of patientid in example.sh.

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.