Henri Wathieu
Henri Wathieu

Reputation: 121

Passing a pre-defined variable from shell script to shell script in Linux

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.

Upvotes: 1

Views: 611

Answers (3)

Jens
Jens

Reputation: 72619

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, ...)

Upvotes: 1

Wintermute
Wintermute

Reputation: 44023

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

Upvotes: 1

Marat Talipov
Marat Talipov

Reputation: 13304

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.

Upvotes: 0

Related Questions