# Passing variable from called script to the caller script

**URL:** <https://community.unix.com/t/passing-variable-from-called-script-to-the-caller-script/346549>\
**Category:** Shell Programming and Scripting\
**Created:** [September 22, 2014, 2:29pm UTC](https://community.unix.com/t/passing-variable-from-called-script-to-the-caller-script/346549 "2014-09-22T14:29:00Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![LoneRanger](https://community.unix.com/user_avatar/community.unix.com/loneranger/32/9162_2.png) [@LoneRanger](https://community.unix.com/u/LoneRanger)\
**Post date:** [September 22, 2014, 2:29pm UTC](https://community.unix.com/t/passing-variable-from-called-script-to-the-caller-script/346549/1 "2014-09-22T14:29:00Z")

</div>

Hi all,

Warm regards!

I am in a difficult situation here. I have been trying to create a shell script which calls another shell script inside. Here is a simplified version of the same.

Calling Script.

```nohighlight
#!/bin/ksh

# want to run as a different process
/path_to_called_script/called_script.ksh 10 20

```

#some other operations

called script

```nohighlight
#!/bin/ksh

sqlplus -s ${connection} <<EOF

sample_procedure($1,$2);
exit;
EOF

```

problem is $1 and $2 is coming as empty . it contains nothing. What's the correct procedure to pass variable into sqlplus from shell script? please help.

let me know in case additional info is needed.

Shell in use -ksh

---

<div class="post-metadata">

**Author:** ![Corona688](https://community.unix.com/letter_avatar/corona688/32/5_5575768a8748004e209b776fc1b2916d.png) [@Corona688](https://community.unix.com/u/Corona688)\
**Post date:** [September 22, 2014, 2:51pm UTC](https://community.unix.com/t/passing-variable-from-called-script-to-the-caller-script/346549/2 "2014-09-22T14:51:45Z")

</div>

If I replace 'sqlplus' with 'cat' it obviously works:

```nohighlight
$ cat caller.ksh

#!/bin/ksh

# want to run as a different process
./called_script.ksh 10 20

$ cat called_script.ksh
#!/bin/ksh

cat -s ${connection} <<EOF

sample_procedure($1,$2);
exit;
EOF

$ ./caller.ksh

sample_procedure(10,20);
exit;

$

```

So either these aren't your actual scripts, or the error lies elsewhere.

Remember that `set -- ...` can alter the values of $1 $2 ...

---

<div class="post-metadata">

**Author:** ![LoneRanger](https://community.unix.com/user_avatar/community.unix.com/loneranger/32/9162_2.png) [@LoneRanger](https://community.unix.com/u/LoneRanger)\
**Post date:** [September 22, 2014, 3:15pm UTC](https://community.unix.com/t/passing-variable-from-called-script-to-the-caller-script/346549/3 "2014-09-22T15:15:29Z")

</div>

I am calling the script using the notation /path/called\_script.sh

and not ./called\_script.sh

do you think it's making a difference?

---

<div class="post-metadata">

**Author:** ![Corona688](https://community.unix.com/letter_avatar/corona688/32/5_5575768a8748004e209b776fc1b2916d.png) [@Corona688](https://community.unix.com/u/Corona688)\
**Post date:** [September 22, 2014, 3:19pm UTC](https://community.unix.com/t/passing-variable-from-called-script-to-the-caller-script/346549/4 "2014-09-22T15:19:23Z")

</div>

No, it does not.

Either these aren't your actual scripts, or the error lies elsewhere.

---

<div class="post-metadata">

**Author:** ![LoneRanger](https://community.unix.com/user_avatar/community.unix.com/loneranger/32/9162_2.png) [@LoneRanger](https://community.unix.com/u/LoneRanger)\
**Post date:** [September 22, 2014, 3:26pm UTC](https://community.unix.com/t/passing-variable-from-called-script-to-the-caller-script/346549/5 "2014-09-22T15:26:50Z")

</div>

This is being called like this

```plaintext
${MMHOME}/external/scripts/belk_nil_prc_thread.ksh 10 20

```

##Inside belk\_nil\_prc\_thread.ksh

```plaintext
function EXEC_BELK_NEW_ITEM_LOC_PRICING
{
   echo "set feedback off
      set heading off
	  set echo off
      set serveroutput on size 1000000

      VARIABLE GV_return_code NUMBER;
      VARIABLE GV_script_error CHAR(255);
	  VARIABLE GV_process_flag CHAR(10);

      EXEC :GV_return_code := 0;
      EXEC :GV_script_error := NULL;
	  EXEC :GV_process_flag := 'TRUE';

      WHENEVER SQLERROR EXIT ${FATAL}
      
      BEGIN
         BELK_NEW_ITEM_LOC_PRICING($1,$2,:GV_script_error,:GV_process_flag);
		 
	  
      EXCEPTION
         
         WHEN OTHERS THEN
		     ROLLBACK;
             :GV_return_code := ${FATAL};
      END;
        
	 
	   print :GV_process_flag;
       print :GV_script_error;
	   exit :GV_return_code;
       /" | sqlplus -s ${connectStr} >> ${errTemp}
	   
    if [$? -eq ${FATAL}}]; then
	
      echo "${pgmName}:FAILED at EXEC_BELK_NEW_ITEM_LOC_PRICING() with error ${errTemp} !" >>${ERRORFILE}
      
      LOG_MESSAGE "${pgmName}:EXEC_BELK_NEW_ITEM_LOC_PRICING() Failed !"
    
      return ${FATAL}
	fi
	
		
}

```

But $1 and $2 is being passed as EMPTY.

---------- Post updated at 02:26 PM ---------- Previous update was at 02:23 PM ----------

> [@corona688](#):
>
> No, it does not.
> 
> Either these aren't your actual scripts, or the error lies elsewhere.

But I was reading an article which says giving .(dot) runs the called\_script in the same process ID whereas /path/called\_script.sh runs in its own process ID which is different than the caller script's PID.

---

<div class="post-metadata">

**Author:** ![Chubler\_XL](https://community.unix.com/user_avatar/community.unix.com/chubler_xl/32/2077_2.png) [@Chubler\_XL](https://community.unix.com/u/Chubler_XL)\
**Post date:** [September 22, 2014, 4:29pm UTC](https://community.unix.com/t/passing-variable-from-called-script-to-the-caller-script/346549/6 "2014-09-22T16:29:40Z")

</div>

The problem is that within the function EXEC\_BELK\_NEW\_ITEM\_LOC\_PRICING  
in your script $1 and $2 will be the positional parameters to the function not the script. You could try:

```nohighlight
#!/bin/ksh

priceA=$1
priceB=$2

...

function EXEC_BELK_NEW_ITEM_LOC_PRICING
{
   echo "
    ....

    BEGIN
    BELK_NEW_ITEM_LOC_PRICING($priceA,$priceB,:GV_script_error,:GV_process_flag);
    ... 
    " | sqlplus -s ${connectStr} >> ${errTemp}
...
}

```

. (dot) only runs in current process ID, if you use it as command (on it's own), this is could "sourcing" because the single dot can also be replaced with the keyword source examples:

```nohighlight
. /path/to/script
. ./script

source /path/to/script
source ./script

```

---

<div class="post-metadata">

**Author:** ![Corona688](https://community.unix.com/letter_avatar/corona688/32/5_5575768a8748004e209b776fc1b2916d.png) [@Corona688](https://community.unix.com/u/Corona688)\
**Post date:** [September 22, 2014, 5:09pm UTC](https://community.unix.com/t/passing-variable-from-called-script-to-the-caller-script/346549/7 "2014-09-22T17:09:09Z")

</div>

> [@loneranger](#):
>
> But I was reading an article which says giving .(dot) runs the called\_script in the same process ID whereas /path/called\_script.sh runs in its own process ID which is different than the caller script's PID.

dot-space means, to the shell, "run it in the current shell".

dot-slash means, to the operating system, "the current directory". It has no special meaning to the shell.

Thank you for finally posting your real script, which has revealed the actual problem.
