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
function foo { ...; }
syntax from ksh actually precedes thefoo() 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