开发者

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

开发者 https://www.devze.com 2023-02-20 22:38 出处:网络
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

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号