Comments on Shell Scripting Part V: Functions in Bash
Welcome to part 5 of HowToForge's shell scripting tutorial series. In this part, you will learn how to efficiently structure your scripts by creating functions. By the end of this tutorial, you will be able to know how to create functions in the Linux Bash Shell, pass parameters to your functions and return some values from a function to your main code.
8 Comment(s)
Comments
I like subshells. In a bash shell script, a subshell is code that is surrounded by parentheses. It runs as a separate process. You can see it in the process table. A subshell can be backgrounded & redirected like any other code, and subshells can be nested like russian dolls. This takes advantage of inheritance, as explained in 'man fork'. If you want to do 2 or more things at once in a shell script, like create/update a logfile and at the same time monitor that logfile, you will need at least one subshell.
Examples: everyone needs a timer. Zenity required. http://user.cavenet.com/rolandl/timer.txt
Custom action for your file manager: http://user.cavenet.com/rolandl/ftp-user.txt
typo on the function syntax?
syntax 1:
function function_name
{
##commands
}
syntax 2:
function_name ()
{
##commands
}
As far as I can see, the example is ok. You can define a function with and without the keyword "function" in front of the function name in the bash shell.
I'm pertaining to the use of () which should not be present on the first syntax.
Thanks, I corrected that.
Can bash functions export variables from within functions?
Yes it can.