Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: How do you decide whether to make something a function or a script?



On 09/08/2018 07:22 PM, TJ Luoma wrote:
But it's gotten me to wonder:

That can be dangerous.  ;-)

Why make any functions at all? Why not just make them all scripts? If they are scripts in the $PATH then you can just call them by name and they work in the login shell or in scripts. If they are functions, you have to autoload them first.

So I've gotten to wonder… what makes you make something a function vs a script?

IMHO it all has to do with scope. I tend to progress along the following sequence when I have reason to move to the next level:

1)  alias
2)  function
3)  script

IMHO aliases enable me to be lazy and not type things out completely, or make what seems to be custom commands like; alias myserver='ssh myserver'.

I upgrade from aliases to functions any time I need parameters or I want scripts to be able to use the same command.

I upgrade from functions to scripts when I want to be able to run the command from outside of the shell, like an ssh remote command: ssh myserver openDB. The remote command doesn't launch the shell, thus doesn't have access to functions, much less aliases. But it does have access to scripts if I have my PATH configured properly.



--
Grant. . . .
unix || die

Attachment: smime.p7s
Description: S/MIME Cryptographic Signature



Messages sorted by: Reverse Date, Date, Thread, Author