passing of a varibale to subshell


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting passing of a varibale to subshell
# 1  
Old 05-25-2008
passing of a varibale to subshell

Hi All,

I need some info.

Could you please tell me how to use the variable of a parent shell in the subshell. Also can we modify the variable in the subshell ? If yes, will the modified variable visible in the parent shell

I am using two prg.

a.sh
#!/usr/bin/ksh
temp_var="abhishek"
b.sh
echo $temp_var


b.sh
#!/usr/bin/ksh
source a.sh
temp_var="sdfdf"

The source keyword in the b.sh is giving me error.


Thanks in advance
Abhishek Gera
# 2  
Old 05-25-2008
The environment of a parent process is inherited by the child process but not visa versa.
An example to achieve this is:

first.sh:

Code:
#!/bin/ksh

var=100
echo "Before : $var"
var=$(. ./second.sh)
echo "After: $var"

second.sh:

Code:
#!/bin/ksh

let "var = var + 50"
echo $var

Regards
# 3  
Old 05-25-2008
Thanks for the reply....

Also can you suggest me how to make a variable as global variable in the shell script.

Like if we are running programs like a package, how can I access a variable defined in any other script ?


Thanks in advance
Abhishek Gera
# 4  
Old 05-25-2008
If you want to share variables with scripts written by yourself maybe named pipes is a better alternative.
Google for "named pipes in shell".

Regards
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to pass IF condition via shell varibale in awk?

Hello, I have one query regarding passing IF condition shell variable inside awk. Here is the case- File content of keydefn.exp 201~2~LM Limit 02-current value~Limit 02 ~Limit02~Current~Value ~N~Y~S~0~9999999 201~3~LM Limit 03-current value~Limit... (2 Replies)
Discussion started by: anillambait
2 Replies

2. UNIX for Dummies Questions & Answers

set varibale to be output of a command

Hi there! :) How to set varibale to be output of a command in csh. I was using set i="date+'%y%m%d'" but the output is date+'%y%m%d' and without quites and with a single quote the output is the same :wall: :eek: Thanks in advance (2 Replies)
Discussion started by: FUTURE_EINSTEIN
2 Replies

3. Shell Programming and Scripting

Basename in subshell

Hi All, I would like to improve my bash scripting skill and found a problem which I do not understand. Task is to search and print files in directory (and subdirecories) which contains its own name. Files can have spaces in name. This one works fine for files in main directory, but not for... (4 Replies)
Discussion started by: new_item
4 Replies

4. Shell Programming and Scripting

getopts in a subshell

Hello, I've a little problem with one of my ksh scripts and I manage to narrow it to the script here: #!/bin/ksh writeLog() { paramHandle="unknown" OPTIND=1 while getopts :i: option $* do case $option in i) paramHandle=${OPTARG} ;; esac done echo... (2 Replies)
Discussion started by: Dahu
2 Replies

5. Shell Programming and Scripting

Killing a subshell

I am calling a script from with another script and reading its output one line at a time (using <childscript> | while read line) in the parent script. If the output exceeds a predefined number of lines I want to kill the child shell from within the parent shell. I decided to print the process ID... (2 Replies)
Discussion started by: slash_blog
2 Replies

6. Shell Programming and Scripting

Passing arguments to the subshell

I have a shell script which is invoked by passing an argument. The outer shell script calls another subshell and I want the argument passed down to flow down to the subshell. E.g Invoking a shell ======>> abc_refresh.ksh NM Below is the content of abc_refresh.ksh Value1=$1... (7 Replies)
Discussion started by: Mihirjani
7 Replies

7. Shell Programming and Scripting

How to use a varibale in sed

i wanna insert a row in a file. I m using this command to insert a row in the 13th line. sed '13i\ NewRow' MyFile > temp.txt mv temp.txt to MyFile this works fine. now i wanna get the line no using grep into a variable and then use tht variable to insert a new row.. how to do this wid... (8 Replies)
Discussion started by: St.Fartatric
8 Replies

8. Shell Programming and Scripting

Varibale to NULL or ZERO

I have a variable called V_param1 and does it have some value. I want to check this varibale is NULL or Zero then exit else do How to incorported this in Script. any input (2 Replies)
Discussion started by: u263066
2 Replies

9. Shell Programming and Scripting

passing result of query to a varibale in ksh script

Hi, I have a scenario where in i have to extarct max of one column and pass it to a variable. i have tried to export the result as .dat file and read from that file.But my database is mainframe and it is not permitting me to export in .dat file.I have tried using .ixf file but reading from... (2 Replies)
Discussion started by: ammu
2 Replies

10. Shell Programming and Scripting

Subshell Question

The profile of the user is empty. Then before I run the script I want I run a parameter file that populates the variables for oracle. ORACLE_HOME ORACLE_BASE ORACLE_SID PATH etc ... But it seems that these variables are not making it to the shell I am in because when I do an echo on... (6 Replies)
Discussion started by: lesstjm
6 Replies
Login or Register to Ask a Question