0

I couldn't really find an answer online. I'm sure there is a good reason. Does bash not have function parameters? I understand you can pass arguments to it using $1 $@ etc... just found it strange that I have never seen code with anything inside the parentheses.

Expected:

function example_f (var1,var2) { echo "$var1" ; echo "$var2" ; }
example_f("Hello World","Test") 

Real World:

function example_f () { echo "$1" ; echo "$2" ; }
example_f "Hello World" "Test"
0xKate
  • 211
  • 1
  • 8
  • See https://wiki.bash-hackers.org/scripting/obsolete -- the `function` keyword exists for compatibility with pre-POSIX (which is to say, pre-1992) ksh releases. One should use *either* `function funcname {` with no parens for legacy ksh compatibility, or `funcname() {` for POSIX compatibility; when combining both, you're not compatible with *either* legacy ksh *or* the POSIX sh specification. – Charles Duffy Feb 20 '20 at 00:13

1 Answers1

1

The function keyword is a bash extension. The standard syntax for function definitions is simply:

name() { body }

and the parentheses are needed to recognize that this is a function definition rather than an ordinary command invocation. They're not used to contain parameters, they just distinguish the syntax.

bash added the function keyword at the beginning. When you use this, the parentheses are optional, so you can write

function example_f { echo "$var1" ; echo "$var2" ; }
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • Ok thanks, I did see that you could omit it in certain situations just wanted to make sure there is no use of them other than syntactical correctness. – 0xKate Feb 19 '20 at 23:51