I have a script that can be sourced in bash or ksh or it runs in bash if executed. The script stores a desired return code in ${_rc}. I can return $_rc but that leaves _rc defined in the tty. I can unset _rc afterward but that overrides the return code. If the script is not sourced I need to use exit instead of return or i get an error message
I have the following code at the end of my script
cleanup_script # unsets all variables except _rc and _was_sourced
if [ "${_was_sourced}" == 1 ];
return ${_rc}
else
exit ${_rc}
fi
If I run this command
typeset > before.txt
. ./myscript
echo $?
typeset > after.txt
diff before.txt after.txt
I get (in addition to variables and functions defined by the script that I want available to the terminal)
226a236
> _rc=0
232a243
> _was_sourced=1
Which I desire to not have if possible
_was_sourcedbeforereturn, but I don't think there's any way to get rid of_rc.$?value. Problem, though, is that then you have the function escaping into the outer shell... but maybe that's considered less messy?_rcto_-- that way it's a variable that'll be overwritten by the interactive shell's regular operation.