This returns the id of the current shell twice so outputs:
4201
4201
I'd like to change this script so the function is called in a different subshell each time, giving a different shell id for each function call.
i.e. output will be something like:
4201
4204
Does anyone know how I can do this? Is there a way to execute a function in a seperate subshell? Or, can I open a new subshell, call the function and then exit back to the parent shell all from within the script??
I could put the function into a seperate new script and then run this script twice from my original script. This would run the new script in a new subshell. However I would rather keep the code in the original script if possible.
You cannot call a function across shell, but you always have the option to open a sub shell in your script and execute the script again. Be cautious while doing so or you can end up writing a recursive function.
It's because you are calling a comelete command not a funtion. What was asked in the original post was something like calling a function across shells, which doesnot seems to be possible as a sub shell will not be aware of the function.
Seems that the shell does'nt update the value of $$
The following code x.sh :
func1()
{
(
ps -f
echo $$
)
}
func1
func1
gives the ouput :
UID PID PPID C STIME TTY TIME CMD
gssjgu 27864 30168 1 14:30:55 pts/3 0:00 ps -f
gssjgu 30168 32486 0 14:30:55 pts/3 0:00 sh x.sh
gssjgu 32486 34188 1 14:30:55 pts/3 0:00 sh x.sh
gssjgu 34188 27930 0 Aug 09 pts/3 0:01 -ksh
32486
UID PID PPID C STIME TTY TIME CMD
gssjgu 27866 30170 1 14:30:55 pts/3 0:00 ps -f
gssjgu 30170 32486 0 14:30:55 pts/3 0:00 sh x.sh
gssjgu 32486 34188 1 14:30:55 pts/3 0:00 sh x.sh
gssjgu 34188 27930 0 Aug 09 pts/3 0:01 -ksh
32486
The pid displayed is the same 32486 but the echo command is executed in two different subshells of which the pid is displayed as the parent pid of the ps command.