standalone shell script vs. shell function?
Use functions for things you're going to use often. They take up memory and require parsing when the shell starts — parsing which is wasted if you never use the function.
Use scripts for things that take a long time, which you do seldom. The per-invocation parsing time is negligible and the clarity that having the scripts separate brings is beneficial.
So, for backup scripts, I'd strongly recommend a script rather than a function.
A python-like solution:
#!/bin/bashcaller_shlvl=$1shiftbackup () { ...}if (( $caller_shlvl < $SHLVL )); then backup "$@"fi
You can do one of two things:
bash script.sh $SHLVL other args
to run the backup when you call the script, or
source script.sh $SHLVL# Time passesbackup firstarg secondarg etc
which will define the function in the current shell but not execute it.
(Having to pass $SHLVL as the explicit first argument is the closest I could get to simulating Python's
if __name__=="__main__":
It's not pretty, and probably makes this answer not-so-useful.)