maihabunash
maihabunash

Reputation: 1702

bash - two ways to write function syntax

in bash we can write function , but actually we have two ways ( two syntax ) ,

Please advice

What the differences between example1 to example2

Or maybe there are no any differences?

 function I_love_function

 {

  .
  .

 }

I_love_function ()

{

  .
  .
}

How to call the function I_love_function

Upvotes: 2

Views: 787

Answers (2)

konsolebox
konsolebox

Reputation: 75568

I_love_function () { } is compatible with POSIX/original sh-based shells but if your script is only meant to run in Bash, then there won't be much difference and more of just a preference.

Personally I prefer function func_name { } format since () seems meaningless to me as it doesn't explicitly specify the accepted arguments to the function.

Upvotes: 1

Didac Montero
Didac Montero

Reputation: 2086

From: http://www.gnu.org/software/bash/manual/bashref.html#Shell-Functions

Functions are declared using this syntax:

name () compound-command [ redirections ] 
or 
function name [()] compound-command [ redirections ]

This defines a shell function named name. The reserved word function is optional. If the function reserved word is supplied, the parentheses are optional.

Upvotes: 2

Related Questions