Using Functions in Shell Scripts
You’ll be amazed at how functions can make your shell scripts so much more functional. (Yeah, I know. That really was a bad pun.) Read on to see how to make that happen.
Creating and Calling Functions
When you place a function inside a shell script, the function code won’t get executed until you call the function. Let’s modify the howdy_func.sh
script to make that happen, like this:
#!/bin/bash
howdy ()
{
echo "Howdy, world!";
echo "How's it going?"
}
howdy
You see that all I had to do was to place the name of the function outside of the function code block. Also, note that the function definition must come before the function call. Otherwise, the shell will never be able to find the function. Anyway, when I run the script now, it works fine:
[donnie@fedora ~]$ ./howdy_func.sh
Howdy, world!
How's it going?
[donnie@fedora ~]$
I should also point out that the first curly...