bash function parms format error

function()
{
parm1=$1
parm2=$2
parm3=$3
}

function 1 2 3

I’ve been using this format. Another version has this format. I can’t get it work, “Error command not found” Can this format work with bash?

https://stackoverflow.com/questions/6212219/passing-parameters-to-a-bash-function

function example { args : string firstName , string lastName , integer age } {
  echo "My name is ${firstName} ${lastName} and I am ${age} years old."
}

example test1 test2 test3

function example {
  args
    : @required string firstName
    : string lastName
    : integer age
    : string[] ...favoriteHobbies

  echo "My name is ${firstName} ${lastName} and I am ${age} years old."
  echo "My favorite hobbies include: ${favoriteHobbies
[li]}"[/li]

}

Which (Bash) function definition is not functioning as expected?
This one:

name () compound-command [redirection]

Or, this one:

function name ()] compound-command [redirection]
  • Please note that, when using the 2nd definition – which uses the key word “function” – the parentheses are optional …

@lord_valarian:

Or, are you asking about parsing positional parameters which are provided when the function is called?

  • If this is the case then, take a look at the Bash built-in “getopts”.
  • There’s also the older “getopt” which used to have problems with white-space and special characters but, no longer …

@lord_valarian:

Or, are you asking about the Bash builtin “:”?

: [arguments]

does nothing beyond expanding arguments and performing any specified redirections

This gives command not found.

function example { args : string firstName , string lastName , integer age } {
  echo "My name is ${firstName} ${lastName} and I am ${age} years old."
}

The procedure doesn’t give an error.

example test1 test2 test3

This gives command not found. ??

Hi,

If you have ***args ***defined that can handle/parse the arguments then sure, otherwise the error message is correct. There is nothing in your path (an executable or a function, alias or a builtin) that has the name ***args ***. Also the first function in the above example should be avoided since it is not documented, while it may work because it is still a compound command it is not easy to read or understand by someone who is reading your code, unless of course you’re code golfing…

I’ve checked through some old documentation (books – paper) – “UNIX for the impatient”; “UNIX System V”; “O’Reilly Bash”; – and, there’s no mention of the following StackOverflow function syntax:

function name { «named parameters» } { «commands» }

In other words, this syntax seems to have never been part of the Bash definition.

  • It may well be that, at some stage in the Bash development, there were a couple of Bash versions limited to one or two special sites, located in some country or another, which may have supported this function syntax relating to named function parameters but, it seems that, the part of this planet with those special Bash versions has, disappeared

Did you try to actually read StackOverflow post where this “alternative syntax” was described?

Yes.

If you prefer named parameters, it’s possible (with a few tricks) to actually pass named parameters to functions (also makes it possible to pass arrays and references).

And yes, it’s part of the “Bash Infinity” framework – last change in GitHub over a year ago – last change on the “Invent Life” web site: 2015 …

  • Yes, it was an interesting attempt to move Bash into the O-O world, but …
  • There ain’t a package for this in the openSUSE repositories and, DuckDuckGo searching for an RPM package anywhere doesn’t result in anything except the GitHub entry …

When one looks for constructs found in proper programming languages, my limited experience with bash tells me it would be time to switch to a proper programming language. Python 3.x should be omnipresent these days.

Whether or not Object-Oriented programming is “better than” programming which is not O-O has been long discussed, and criticised – since the idea of O-O first appeared …
Yes, Python is O-O, as is Perl and, Ruby and, … – the list is long …

  • Even Fortran is O-O these days – as of Fortran 2003 …

Bottom line:

  • Bash ain’t O-O – C also not …
  • If you want an O-O scripting language, you have a large number of choices to choose from – Bash ain’t one of them …