Is it possible to source a Bash snippet, but only actually provide a function from inside it if a certain condition holds true?
So what I am asking is that I can unconditionally source all files from a directory, but the sourced files contain the logic to provide functions to the sourcing shell or not.
Example:
.bashrcsources whole sub folder.bashrc.d.bashrc.d/xyzprovides a functionadduser2groupwhich works on old systems whereusermoddoesn’t handle-a -G.bashrc.d/xyzshould provide that function only to the sourcing shell if it’s running on such old system.
My current method is to conditionally create an alias named adduser after the Debian program (alias adduser=adduser2group). So I only implement the adduser <user> <group> semantics, but still it’s helpful.
Is there a solution that doesn’t require this workaround? After all this method means a chance for name clashes which I’d prefer to avoid.
You can define the functions you want and, whenever a particular condition holds, you just unset the function:
Evaluating your condition — and considering it holds true: