I do know the JavaScript syntax:

var functionName = function(some perameter){

some code here; };

but what is the syntax for a function in bash?

  • 2
    You are getting downvoted because you can find an answer to this with 5 minutes worth of web search. In general, questions that can be answered with RTFM get downvoted. – terdon Jan 21 '13 at 19:06

There are two ways to create Bash functions:

function function_name { 
  # your commands


function_name () { 
  # your commands

Both are equivalent. Read more about it, for example in the Advanced Bash-Scripting Guide under Functions or in the Bash Reference Manual.

  • Three ways, technically: function foo () { is also allowed. Only one of the keyword function and the parentheses are required, though. The function keyword is a bash extension, though; your second example is the standard way of defining a function and will work in any POSIX-compatible shell. – chepner Jan 21 '13 at 22:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.