[SOLVED] How differentiate functions with same name in separate shells
Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
How differentiate functions with same name in separate shells
Hi, here is my question.
I have a parent shell that will call a function in subshell and return back to the parent.
The next line of code will call a function in the parent (This function name ALSO exits in the subshell)
It seems the function in the subshell is getting executed.
For simplicity, I created small test shells to play around with:
1. Parent shell - calling.sh
DUPL_FUNC()
{
echo "In DUPL_FUNC in calling.sh"
}
. func.sh FUNC_FUNC abc
DUPL_FUNC
exit 0
2. subshell - func.sh:
DUPL_FUNC()
{
echo "In DUPL_FUNC in func.sh"
}
FUNC_FUNC()
{
ARG1=$1
echo "in FUNC_FUNC in func.sh"
}
FUNC_NAME=$*
$FUNC_NAME
return 0
3. Execute:
./calling.sh
in FUNC_FUNC in func.sh
In DUPL_FUNC in func.sh
I want the echo from DUPL_FUNC to come from the parent, calling.sh like such:
In DUPL_FUNC in calling.sh
Distribution: Void, Linux From Scratch, Slackware64
Posts: 3,150
Rep:
Basically you can't, a subshell can't know anything about a parent process except for any environment variable's exported from the parent and if the child alters a variable the parent can't know the new value, if the child exports a variable the parent can't know the value, though any children of the child will know about the newly exported variable.
If you need to share functions use the "." or include statements, if you then need to do different things in the function depending on the calling process just pass an argument to the function, either the pid of the calling process or something similar.
Please also use code tags to surround your code as it make it easier to read.
I have a parent shell that will call a function in subshell and return back to the parent.
This is not what you did.
Code:
. func.sh FUNC_FUNC abc
Here you are sourcing (by the use of '.') func.sh, which if it has the same named function as the calling script, will overwrite it, so now only DUPL_FUNC from func.sh exists.
To make it run in a subshell, simply call it:
Code:
func.sh FUNC_FUNC abc
Of course if not in your PATH you will need to give the path to the script.
keithhedger@LFSHal:/tmp-> ./proc1
testfunc from proc1
./proc2: line 9: testfuncproc1: command not found
As you can see proc2 ( the child ) cant see the parents function defs.
Though in bash you can export functions and they're passed to the children through the environment (which was what led to the 'Shellshock' vulnerability thingy a while back).
Code:
test@ws2:/tmp$ cat proc1
#!/bin/bash
testfunc1() {
echo "testfunc1 from proc1"
}
testfunc1
export -f testfunc1
./proc2
test@ws2:/tmp$ cat proc2
#!/bin/bash
testfunc1
test@ws2:/tmp$ ./proc1
testfunc1 from proc1
testfunc1 from proc1
test@ws2:/tmp$
Exporting functions is a bash specific extension however. Traditional bourne shell and ksh had the good sense not to provide this "feature". When I remember, I make a point of using "#!/bin/bash -p" to disable it.
Distribution: Void, Linux From Scratch, Slackware64
Posts: 3,150
Rep:
Quote:
Originally Posted by GazL
Though in bash you can export functions and they're passed to the children through the environment (which was what led to the 'Shellshock' vulnerability thingy a while back).
...
Exporting functions is a bash specific extension however. Traditional bourne shell and ksh had the good sense not to provide this "feature". When I remember, I make a point of using "#!/bin/bash -p" to disable it.
You are of course quite correct I forgot about the -f switch to export, but as you say is non statndard.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.