The difference between searching and forcing is that searching for the source executes the called script in the calling process. Henk showed an elegant solution in ksh93, but if, like me, you are stuck with ksh88, then you need an alternative. I would prefer not to change the default ksh method using the C-shell syntax, and at work this would be against our coding standards, so creating and using the source () function would not work for me. ps, $ 0 and $ _ are unreliable, so there is an alternative here:
$ cat b.sh; cat c.sh; ./ b.sh
#!/bin/ksh export SCRIPT=c.sh . $SCRIPT echo "PPID: $$" echo "FORKING c.sh" ./c.sh
If we set the script to be called into a variable and correct it with a variable, this variable will be accessible to the script being called, since they are in the same process space.
#!/bin/ksh arguments=$_ pid=$$ echo "PID:$pid" command=`ps -o args -p $pid | tail -1` echo "COMMAND (from ps -o args of the PID): $command" echo "COMMAND (from c.sh \$_ ) : $arguments" echo "\$SCRIPT variable: $SCRIPT" echo dirname: `dirname $0` echo ; echo
The output is as follows:
PID:21665 COMMAND (from ps -o args of the PID): /bin/ksh ./b.sh COMMAND (from c.sh $_ ) : SCRIPT=c.sh $SCRIPT variable: c.sh dirname: . PPID: 21665 FORKING c.sh PID:21669 COMMAND (from ps -o args of the PID): /bin/ksh ./c.sh COMMAND (from c.sh $_ ) : ./c.sh $SCRIPT variable: c.sh dirname: .
Therefore, when we set the script variable in the calling script, the variable is either accessible from the operands of the script, or, in the case of a forked process, the variable, along with all other environment variables of the parent process, is copied to the child process. In any case, the script variable may contain your command and arguments and will be available in the case of both sources and forking.