Search code examples
bashdarwin

What is the syntax for writing a function in Darwin (OS X)?


I tried

function myfunc() {stuff goes here;}

to no avail. Just trying to put a basic sql-dump script in my .profile file.


Solution

  • 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