Don't read this closely, there's only one thing that's relevant, see  below:


# Create a directory:
_overlay_mkdir ()
{
    # Create backup directory if it hasn't already been done.
    [ -d $1 ] && { infomsg "\"$1\" already exists."; return 0 }

    mkdir -vp $1 ||
        { errormsg "Couldn't create directory: \"$1\"." && return 1 }
    return 0
}

# Synchronize two directories:
_overlay_rsync ()
{
    actionmsg "Synching \"$1\" to \"$2\" ..."
    # greysky uses: --delete-after

    rsync -aX --del --inplace --no-whole-file $1/* $2 ||
        { errormsg "Can't rsync \"$1\" to \"$2\"." && return 1 }
    return 0
}

# Run for each directory to be overlaid:
overlay ()
{
    [[ ! "$1" || "$1" == '-h' ]] && _overlay_syntax && return 0
    
    # NB, this check must run before initializing OR normal run.
    # '-x' = exact match. Note, even the firefox 'can't find profile' error msg. box triggers this:
    [ -n "$( pgrep -x ${program} )" ] &&
    errormsg "\"${program}\" is running, please quit before continuing." && return 1

    actionmsg "\nInitializing overlay for \"$1\" ..."

    local subject="$1" # Full path of directory to be overlayed.

    # renamed subject dir. (must borrow real name to create link to overlay).
    local active="${subject}-active"
    local backup="/aMisc/Overlay$subject"    # Backup.
    local target="/run/user/0$subject"        # The overlay directory.
    local working="${target}-working"        # Used by 'mount' internally.


# Rescue if we did not unoverlay properly, as when computer is shut down without quitting facebook first.  If subject is a link pointing to target we almost surely did an improper shut down:
[[ -h "$subject" && $( readlink -m "$subject" ) == "$target" ]] &&
{
    # Recreate target:
    _overlay_mkdir ${target} || return 1
    # And remount:
    mount -vt overlay -o lowerdir=${active},upperdir=${target},workdir=${working} overlay ${target} || return 1
    return 0
}

    [[ -d "$subject" ]] ||
        { errormsg "\"${subject}\" seems not to be a directory,\n but we can only overlay directories!"; return 1 }

    # rsync subject to the backup directory: Do this here so that we get the backup no matter what.  This way the pointer does not exist yet.
    _overlay_mkdir ${backup}  || return 1
    _overlay_rsync "$subject" "$backup" || return 1
    
#"$subject" is: |/root/.mozilla|
#"$backup" is:  |/aOverlay/root/.mozilla-backup|
#"$active" is:  |/root/.mozilla-active|
#"$target" is:  |/run/user/0/root/.mozilla|
#"$working" is: |/run/user/0/root/.mozilla-working|

    if [[ -h "$subject" &&  $( readlink -m "$subject" ) == "$target" && \
    -d "$backup" && -d "$active" && -d "$target" && -d "$working" ]]; then
            infomsg "\"${subject}\" overlay is already initialized."
    else
        # Create directories if it hasn't already been done.
        _overlay_mkdir ${target}  || return 1
        _overlay_mkdir ${working} || return 1
    
        # Subject directory is renamed, and ...
        mv -v "$subject" "$active" || return 1
        # ... 'subject' is now a link to overlay.
        ln -sv "$target" "$subject" ||
            { errormsg "Couldn't link \"$subject\" to \"$target\"."; return 1 }

        actionmsg "\nCreating overlay: lowerdir=\"$active\" overlay=\"$target\" ..."

        # -v = verbose, -t overlay = type is overlay.  lowerdir is hidden by upperdir.  workdir is internal to mount.
        mount -vt overlay -o lowerdir=${active},upperdir=${target},workdir=${working} overlay ${target}
    fi
    return 0
}

---------------------------------------------------------------------------------------

... the above is part of a function hierarchy can can be four levels deep and if there's an error (it's being debugged so there's error tests everywhere), I need these 'return 1 ... return 1 ... return 1 ... return 1 ... to break back to the shell.  Now, didn't we discuss something like this a few months back?  ... to do with zmv ... 'err-exit' or something like that.  I was complaining that zmv should NOT crash -- but Bart pointed out that it's not a crash it's a controlled thing, of course.  And zmv was modified accordingly IIRC. 

Anyway, it occurs to me that in the above, I want the opposite -- I DO want any of these errors, however many levels deep it might be, to exit/return directly back to the shell (but 'exit' itself quits the shell entirely) without having to return from each level one at a time.  As always I wish I could search the archives and dig this up, because it was the same issue. 

'err-exit'?  Or can a trap do this? or, if it was run in a subshell then 'exit' would return to the calling shell?