How to export a variable from a subshell to the parent shell?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to export a variable from a subshell to the parent shell?
# 8  
Old 03-26-2012
Quote:
Originally Posted by Scrutinizer
@vomv1988: Thank you for the explanation. You started an interesting thread, I would not say it is criticism, but rather some hopefully helpful comments. Regarding the parentheses in your original script, I was only referring to them because I think they are superfluous...
Yes, in a way, they are, and you are correct to state that the functionality of the original script is not altered by their absence. My intention in putting them there was only to emphasize how a child script (such as a background process, or a parenthesized section in a script) can pass data to it's parent.

I'm very glad you find the thread interesting, and I appreciate the multiple features of different shells you have provided.

EDIT:

Now that I take a good look at it, you're right! The background process...
Code:
echo 'value' > $TMPFIFO &

...is already in a subshell!, so yes, the parentheses come out as completely superfluous... I get you now, loud and clear. In that sense, I guess you could say that the parentheses show that, no matter how deep the ancestry gets, the fifo will always be there to communicate all of it's levels.

---------- Post updated at 02:37 PM ---------- Previous update was at 02:04 PM ----------

Quote:
Originally Posted by Scrutinizer
Code:
FIFO="/tmp/fifo.$$"
if ! [ -p "$FIFO" ]; then mkfifo "$FIFO" fi { exec >"$FIFO"; sleep 2; ps -f ;echo "hello from $FIFO" ;} & var=$(< "$FIFO") rm "$FIFO" echo "$var"

Ok, here; what is the difference between using exec and doing...

Code:
{  sleep 2; ps -f ;echo "hello from $FIFO" ;} > "$FIFO" &

...instead? I seem to get the same results with both. But you say that:

Quote:
Originally Posted by Scrutinizer
If you have more than one write statement, it is better to use exec, otherwise VARIABLE=`cat $TMPFIFO` goes on after the first command writes an EOF and the sub process will not finish.
What do you mean by "goes on after the first command writes an EOF"?

EDIT:

Unless you're talking about using exec instead of using redirection for each write statement (as opposed to for the whole curly braces, which is what I did here). For example, when I tried:

Code:
#!/bin/bash
FIFO="/tmp/fifo.$$"
if ! [ -p "$FIFO" ]; then
  mkfifo "$FIFO"
fi
{ echo 1 > $FIFO ; echo 2 > $FIFO ; echo 3 > $FIFO ; echo 4 > $FIFO ; echo 5 > $FIFO ; } &
var=$(< "$FIFO")
rm "$FIFO"
echo "$var"

I got varying results for each time I ran it. Fifo redirection for each write statement malfunctions here.

EDIT2:

BTW, thanks for the tip on coprocesses, I've never really used them, or even heard about them before, but it's good to know that the option exists.

Last edited by vomv1988; 03-26-2012 at 03:55 PM..
# 9  
Old 03-26-2012
I mean that one should avoid:
Code:
{ sleep 2; ps -f > "$FIFO"; echo "hello from $FIFO" > "$FIFO" ;} &

using exec is just one way of avoiding that...
Code:
{ exec >"$FIFO"; sleep 2; ps -f ;echo "hello from $FIFO" ;} &

regarding EDIT: That's right

Quote:
I got varying results for each time I ran it. Fifo redirection for each write statement malfunctions here.
It does not malfunction, the subprocess opens and closes stdout and therefore the varying results. I used a sleep statement so that the reading process would be reading before the subprocess started opening the pipe...

Last edited by Scrutinizer; 03-26-2012 at 04:17 PM..
This User Gave Thanks to Scrutinizer For This Post:
# 10  
Old 03-27-2012
Quote:
Originally Posted by Scrutinizer
It does not malfunction, the subprocess opens and closes stdout and therefore the varying results. I used a sleep statement so that the reading process would be reading before the subprocess started opening the pipe...
Ahhh... so the trick is in the timing then... Nice one.

---------- Post updated 03-27-12 at 12:57 PM ---------- Previous update was 03-26-12 at 03:08 PM ----------

I recently found this unusual shell notation in a linux journal article:

Code:
command <(compound-list)

This creates a temporal fifo which is inputted whatever output the list of commands compound-list produces, and thus, command treats this output as if it belonged to a file. This way, <(compound-list) is treated as if it were a common filename. This must be an exclusive bash feature, since I don't recall reading of it in any of the shell programming books I've read, and when I tried looking it up in POSIX.1-2008 (not too thoroughly, I admit) I found it also says nothing about this feature. The feature appears in the Bash Reference Manual, under the section 3.5.6. Process Substitution.

Anyway, I thought it was worth mentioning it, since it's loosely related to this thread.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Infinite "while" loop subshell loses current date variable

I have a simple script to log network connectivity to a set of systems. However, as expected the date appended to the log never changes because the new variable is lost when the loop starts again. Can someone clue me in on how to get around this issue? #!/bin/bash LOG=/tmp/netlog... (3 Replies)
Discussion started by: woodson2
3 Replies

2. Shell Programming and Scripting

How do I get variable defined in BASH subshell outside?

I'm a BASH shell user (relatively new) I need to get a variable calculated in a subshell, outside the subshell, when it completes. I can do it, by writing the variable into a file, and then reading the file again when outside the subshell. I've tried lots of things from exporting to environmental... (3 Replies)
Discussion started by: goreilly
3 Replies

3. Shell Programming and Scripting

Set/Export Env Vars from with Shell Script With Input Variable

I have a shell script I want to run that will set environment variables based on the value of an input variable submitted when the shell script is called. For example: $ mgenv.sh prod This would set environment variables for prod $ mgenv.sh test This would set environment variables... (1 Reply)
Discussion started by: brtaylor73
1 Replies

4. Shell Programming and Scripting

Exporting variables from subshell to parent shell

Hi, I was trying to do something where I would be able to export one local variable in a telnet subshell to its parent shell. I found something like this over here, but couldnt exactly understand it :(. I am referring to this part actually: #! /usr/bin/ksh exec 4>&1 tail -5 >&4 |& exec... (4 Replies)
Discussion started by: King Nothing
4 Replies

5. Shell Programming and Scripting

Makefile: Parent - Child Inheritance and export

Hi, I have a number of Makefiles, including a couple of files that I include in Makefiles, a few scripts that are executed through Makefiles, and I have problems with environment variables that are not inherited to the scripts properly. Simplified scenario: rootdir/Makefile: all: ... (1 Reply)
Discussion started by: Shompis
1 Replies

6. Shell Programming and Scripting

How to export a variable from a child process running in background to the parent

Hi All, I have a script which calls a child script with a parameter to be run in the background . childscript.ksh $a & Can any one suggest me how do i export a variable from the child script to parent script? Note that the child script is in background If the child script is in... (3 Replies)
Discussion started by: aixjadoo
3 Replies

7. Shell Programming and Scripting

full path of a file situated either in parent's dir. or parent's parent dir. so on...

hi experts(novice people can stay away as it is no child's game), i am developing a script which works like recycle bin of windows. the problem i am facing is that when ever i am trying to delete a file which is situated in parent directory or parent's parent directory i am unable to... (1 Reply)
Discussion started by: yahoo!
1 Replies

8. Shell Programming and Scripting

Environment Variable Parent PID

I have always used the "$$" environment variable to find the current process number. Is there any similar way or perhaps something else to easily find the parent process number? I realize I could do something like ps and grep for the process and cut or awk out the parent process but I wanted to... (1 Reply)
Discussion started by: scotbuff
1 Replies

9. UNIX for Dummies Questions & Answers

Export command giving Variable Name vs the Value set for the Variable

I'm having an issue when I export within my program. I'm getting the variable name, not the variable value. I have a configuration file (config.txt) that has the values of the variables set as so: set -a export ARCHIVEPOSourceDir="/interfaces/po/log /interfaces/po/data" export... (2 Replies)
Discussion started by: ParNone
2 Replies

10. Shell Programming and Scripting

Perl: export variable to shell

Hi, I am running a series of scripts and I need to transport a particular variable across many scripts. I thougt of defining an environmental variable which I could access through. But I found that the variable dies as soon as the script ends.. Currently I write this variable to a temporary... (2 Replies)
Discussion started by: oldtrash
2 Replies
Login or Register to Ask a Question