On 10/20/06, Philip Ganchev <[EMAIL PROTECTED]> wrote: [...] > That brings up a question I had wanted to raise. Functions are simiar > to scripts in files. For example, instead of defining a "cp" > function, you can write an executable script in ~/bin/cp and put ~/bin > before /bin on the path. I guess they each have relative advantages: > - functions can be defined interactively on the command line > - you know where a script is defined, in case you want to modify it > Have I missed any, and would it be better to not have functions at all? >
The only major difference between external scripts and functions in fish is this: Functions are executed inside the shell process, scripts are executed in another process. The fact that functions are executed inside the shell proper means that you can change the state of the shell in a large number of ways that a script can never do: * A function can edit non-universal variables in the shell * A function can change the working directory, the umask and other per process properties of the shell * A function can access the list of started jobs in the shell Fish scripts can of course do all these things as well, but the difference is that scripts operate on a separate script process, so while you can change the working directory of the script, this will not make any difference for the shell. These and other related powers that a function has means that there are a great number of things a function can do which the shell can't. But it also means that a bug in a function has more destructive power, since it can mess up your shell, while a buggy command will only mess itself up. It should be noted here that in other shells, like zsh and bash, functions are a bit more limited. In some situations, like in command substitutions and in pipelines, shellscript functions are actually executed inside a subshell, meaning they are much more related to scripts than to fish functions in those cases. This is true of builtins as well. A trivial example of this is the following bash command: grep ggg foo.txt | read a b c echo $a You woul expect this to assign some values to the variables a, b and c, but in reality, the 'read' builtin is executed in a separate process, so the values never reach the shell proper. The script will always output only a blank line. Instead, you have to use a command like this: mkfifo tmp_pipe grep ggg foo.txt >tmp_pipe read <tmp_pipe a b c echo $a [...] -- Axel ------------------------------------------------------------------------- Using Tomcat but need to do more? Need to support web services, security? Get stuff done quickly with pre-integrated technology to make your job easier Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642 _______________________________________________ Fish-users mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/fish-users
