Skip to main content

Posts

Showing posts with the label Functions in Bash shell

Functions in Bash shell

Functions in Bash Scripting are a great way to reuse code. In this section you'll learn how they work and what you can do with them. Think of a function as a small script within a script. It's a small chunk of code which you may call multiple times within your script. They are particularly useful if you have certain tasks which need to be performed several times. Instead of writing out the same code over and over you may write it once in a function then call that function every time. Creating a function is fairly easy. They may be written in two different formats: function_name () { #code to execute } or function function_name { #code to execute } The function can be called with just function_name Example1: f() { echo “This is a test function...f” } f Example2: function f { echo “This is a test function...f” } f function_name : The name of the function. Curly braces {} : Enclose the code block. Functions are declared before being called.   Passing Arguments I...