What is the syntax for writing a function in Darwin (OS X)?
I tried
function myfunc() {stuff goes here;}
to no avail. Just tr开发者_StackOverflow社区ying to put a basic sql-dump script in my .profile file.
What you're actually asking is how to write a shell function. The first question is which shell you're using - the default is bash, but if you're not using bash then the syntax you're using won't work.
If you are using bash, then your syntax is fine. I added the following line to my .profile and it works as advertised...
function myfunc() { echo hello; }
After adding that to my .profile and creating a new terminal window (it won't work in existing terminal windows unless you re-run .profile), the following outcome:
$ myfunc
hello
Bash is pretty much the same on Linux and BSD.
Advanced Bash-Scripting Guide - Chapter 24 says:
function function_name {
command...
}
or
function_name () {
command...
}
about functions.
精彩评论