When is it necessary/desireable (probably 2 different questions) to unset
variables created within a script?  I've written a bash function that gleans
most, if not all, variables created within a script, then unsets them.  But,
when it runs, it takes a second or two, which isn't bad, but feels like
forever when waiting for the bash prompt to reappear.  So now I'm wondering
if it's worthwhile or even needed.

I understand that the script variables cannot be exported to the parent
shell, so I guess the question is... if not unset, do script variables still
consume system resources once a script terminates and returns to the shell?

In case it is worthwhile, the function is listed here for those that might
benefit from it.  If it's not worthwhile, well, writing it has been a good
learning experience -- those that read and helped with my removing '$'s and
concatenating variables with <newline> questions will recognize some of the
code.

Thanks!

bd


# Cleanup - unset all variables; if desired, show a closing message, then
exit
cleanup() {

        # Arguments: "custom message"

        # Get all variable names created with 'varname='
        varlist=`cat $0 | grep = | awk '{ print $1 }' | grep = | \
        awk -F "=" '{ print $1 }'`

        # Get variables created with 'for', 'read', ,select', etc.
        for varname in case for read select until while ; do
                # There's a <space><tab> inside the grep'd []s
                morevars=`cat "$0" | grep "^[   ]*$varname " | sed 's/ \[ //
                s/"//g ; s/\\$//g' | awk '{print $2}'`
                varlist=`echo -e "$varlist\n$morevars"`
        done

        varlist=`echo "$varlist" | sort | uniq`

        for vname in $varlist; do
                unset $vname
        done
        unset vname

        if [ -n "$1" ]; then
                clear
                echo "$1"
                echo
        fi
        exit
}




_______________________________________________
Redhat-list mailing list
[EMAIL PROTECTED]
https://listman.redhat.com/mailman/listinfo/redhat-list

Reply via email to