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)

Add comment

Please register in our forum first to comment.

Comments

By: Sarcastic Fringehead

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.

 

By: Sarcastic Fringehead

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

 

By: Kenneth

typo on the function syntax?

syntax 1:

function function_name

{

##commands

}

 

syntax 2:

function_name ()

{

##commands

}

By: till

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.

By: Kenneth

I'm pertaining to the use of () which should not be present on the first syntax.

By: till

Thanks, I corrected that.

By: Mike T

Can bash functions export variables from within functions?

By: thctlo

Yes it can.