Reputation: 28858
I created a shell script which needed some interactivity in it, so I made the following function:
function check(){
echo $1
read ans
if [ -z $ans ] || [ $ans = "y" ]; then
$2 ${*:3}
fi
}
Now I can use this with:
check "shall I greet?" say_hello oz123 stackoverflow
check "Do you leave?" say_goodbye
Given the functions:
function say_hello(){
echo hello $1
echo hello $2
}
function say_goodbye(){
echo "Goodbye..."
}
I came up with check
by a simple trial, proofing if I can give a function name as a parameter to function. Apparently this is working, but I don't understand why. Can you explain why?
Upvotes: 0
Views: 1095
Reputation: 28858
To expand Etan's comment, here is what man bash says exactly:
SHELL GRAMMAR
Simple Commands A simple command is a sequence of optional variable assignments followed by blank-separated words and redirections, and terminated by a control operator. The first word specifies the command to be executed, and is passed as argument zero. The remaining words are passed as arguments to the invoked command.
I wish I knew this earlier, it can make bash scripts much more constructed and easier to write and read.
Upvotes: 1