0

I am freshening up my bash skills. I use the term skills very loosely; I've been outta the game a long time. So, as a way to brush up, I decided to code something utilizing bash/zenity/yad. I read man pages, peroused informative communities like this, and watched a LOT of YouTube videos. Then, I put together a simple nugget that would generate gift certificates for my art studio. As you might expect, it takes user input from zenity, and appends the data to a text file that sits on the cloud. It was simple and worked fine. But then, I remembered I could use functions within bash to jump around more freely in the code. TLDR; I wanted to pass arguments to functions and realized I didn't understand the syntax.

Here's a rough example of what I attempted:

function foo (bar) {
    if [[ ${bar} -eq 1]]; then
        #do_something_here
        #return value_one_here
    else
        #do_something_else_here
        #return value_two_here
    fi 
} 
foo 3
newvar=$?
echo -e "${newvar}\n"

Obviously, this method of passing arguments to a bash function is syntactically erroneous. I now know how to do it correctly, but it did raise the question: Why is there a () in a function declaration? What goes inside the () if anything? I have yet to see a script with arguments inside the (). Why then, are they part of the function syntax? TIA

  • 1
  • @steeldriver, much appreciated. As I indicated, I've been out of IT since the late 90s. I guess I've become a Covid Coder and have much to catch up on. Apparently, per your related link, the syntax: "foo () {}" is the better choice over using the "function" reservation. It's amazing sometimes how inconsistent code can be. One little tweak and it starts a butterfly effect. I guess that's the way technology works, blink and miss it. Thanks for the insight. – endorpheus Mar 05 '21 at 00:15
  • See also: https://askubuntu.com/q/1023942/158442 – muru Mar 05 '21 at 02:18
  • @steeldriver, the function foo { ...; } syntax from ksh actually precedes the foo() cmd syntax from the Bourne shell. The Bourne shell didn't have functions initially. But then again, ksh wasn't as widely available as the Bourne shell until the end of 80s, so it's the Bourne syntax that stuck. – Stéphane Chazelas Mar 05 '21 at 06:15
  • @muru Thanks for the additional resources. I knew I wasn't the only one who wondered about this. I now understand that the () convention is necessary to distinguish the function from the call. I hope they can make more practical use of it in the future. At least it gives a recognizable distinction that there's a function there. thx again – endorpheus Mar 05 '21 at 07:40
  • @StéphaneChazelas thanks for the correction – steeldriver Mar 05 '21 at 12:03

1 Answers1

0

The syntax used for standard function definitions:

funcname() {
  cmds;
}

has been decided by AT&T in 1984 when this feature has been added to the Bourne Shell.

The function syntax:

function funcname {
  cmds;
}

is from ksh from around 1983. This was also at AT&T, but in the group of David Korn that at that time was working on adding database builtin commands into the Bourne Shell and as a side effect created ksh as a modified Bourne Shell.

schily
  • 19,173