Variable passing


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Variable passing
# 1  
Old 01-25-2006
Variable passing

Hi,

If a script A(Parent) is running and script B(child) is run from script A, will the variables in script A be past to script B?
Will the variables exist only for the duration of running the script?

Thank you
# 2  
Old 01-25-2006
When a process (in this case a script) is running, it runs with a certain environment. This environment is passed on to any children that the process may spawn. So any variables that are required to be available to the child process must be present in the parent process' environment.

You can make a variable part of a shell's environment using the export command in sh/ksh/bash or the setenv command in csh/tcsh.
# 3  
Old 01-25-2006
Depends on the way you invoke the script B from A.

See this

Code:
[/tmp]$ cat a.sh
#! /bin/sh
a=10
export b=15
echo "Calling as ./b.sh"
./b.sh
echo "Calling as . ./b.sh"
. ./b.sh
[/tmp]$ cat b.sh
#! /bin/sh

echo "a=[$a]"
echo "b=[$b]"
[/tmp]$ ./a.sh 
Calling as ./b.sh
a=[]
b=[15]
Calling as . ./b.sh
a=[10]
b=[15]
[/tmp]$

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Passing a variable via ssh, can't quite get it right

Hi Guys n Girls, Below im using a while command to wait for a file on another server then carrying on with the script..... I dont believe the $Sausage1 variable is being passed to the other server so its not finding the file. If i replace the variable with the date then it works as expected. ... (2 Replies)
Discussion started by: twinion
2 Replies

2. UNIX for Advanced & Expert Users

Passing variable as input & storing output in other variable

I have a below syntax its working fine... var12=$(ps -ef | grep apache | awk '{print $2,$4}') Im getting expected output as below: printf "%b\n" "${VAR12}" dell 123 dell 456 dell 457 Now I wrote a while loop.. the output of VAR12 should be passed as input parameters to while loop and results... (5 Replies)
Discussion started by: sam@sam
5 Replies

3. Shell Programming and Scripting

Passing a variable value to an option

Hello, I am new to shell (i.e. linux bash) programming and have the following question: When using this wget command I can download a certain website that needs login information by passing a previously acquired cookie: wget --header='Cookie: SID=ac658ef0876b24ff456' somewebsite.comAs... (5 Replies)
Discussion started by: iggy98
5 Replies

4. Shell Programming and Scripting

Passing dynamic variable within another variable.

I have a small program which needs to pass variable dynamically to form the name of a second variable whose value wil be passed on to a third variable. ***************** Program Start ****************** LOC1=/loc1 PAT1IN=/loc2 PAT2IN=/loc3 if ; then for fpattern in `cat... (5 Replies)
Discussion started by: Cyril Jos
5 Replies

5. Shell Programming and Scripting

Passing variable with *

Hi Folks, I would like to pass a variable with a wild card in an argument. My script works if I don't use a wildcard but fails when I use *. I want to use the script like: scriptname -F <filename*> @ i = 0 while ($i <= ${#argv}) switch ($argv) case -F: set j = `echo $i +1... (2 Replies)
Discussion started by: dixits
2 Replies

6. Shell Programming and Scripting

Passing variable to awk

Hi, I'm new with this stuff, but I hope you can help me. This is what I'm trying to do: for id in $var; do awk '{if ($1 == $id) print $2}' merg_data.dat > neigh.tmp done I need that for every "id", awk search the first column of the file merg_data.dat which contains "id" and... (3 Replies)
Discussion started by: matteo86
3 Replies

7. Shell Programming and Scripting

Passing a wildcard in a variable

Hi There I am new to scripting and require some assistance please. I am trying to define a variable with a wildcard in a shell script (.ksh) that will be run on AIX 5300-10. The variable I am trying is: FILES=LLA_*.CSVWhen I run the following section of the script: scp... (2 Replies)
Discussion started by: jimbojames
2 Replies

8. Shell Programming and Scripting

Passing Variable in sed

Dear All, I want to print a file. First I tried with this sed '2q;d' filename it worked. But when i put following it is not working x=2; sed '$xq;d' filename Would any one suggest how to pass the variable? (7 Replies)
Discussion started by: saifurshaon
7 Replies

9. Shell Programming and Scripting

passing a variable inside another variable.

Any help would be great. I know this is a dumb way of doing this, but I would like to know if there is a solution doing it this way. I'm very new at this and I'd like to learn more. Thanks! :D:D count=0 while ; do echo "enter your name" read name_$count let count=count+1 done ... (2 Replies)
Discussion started by: reconflux
2 Replies

10. UNIX for Dummies Questions & Answers

passing a variable inside a variable to a function

I would like to know how to pass a variable inside a variable to a function. sample code below -------------- for x in 1 9 do check_null $C$x ##call function to check if the value is null if then echo "line number:$var_cnt,... (2 Replies)
Discussion started by: KingVikram
2 Replies
Login or Register to Ask a Question