BASH SCRIPTING
Bash Functions
Functions in bash scripting are a great option to reuse code. A Bash function can be defined as a set of
commands which can be called several times within bash script. The purpose of function in bash is to
help you make your scripts more readable and avoid writing the same code again and again. It also
allows the developers to break a complicated and lengthy code to small parts which can be called
whenever required. Functions can be called anytime and repeatedly, which will enable us to reuse,
optimize, and minimize the code.
Following are some key points about bash functions:
• A function has to be declared in the shell script before we can use it.
• Arguments can be passed to the functions and accessed inside the function as $1, $2, etc.
• Local variables can be assigned within the function, and the scope of such variables will only be
that particular function.
• Built-in commands of Bash shell can be overridden using functions.
Syntax
The syntax for declaring a bash function can be defined in two formats:
1. The first method starts with the function name, followed by parentheses. It is the most preferred and
commonly used method:
function_name () {
commands
}
Single line version can be mentioned as below:
function_name () { commands; }
2. The second method starts with the function reserved word, followed by the function name:
function function_name {
commands
}
Single line version can be mentioned as below:
, function function_name { commands; }
Compared to most of the programming languages, Bash functions are somewhat limited. Let?s
understand the concept with the help of some examples:
Example: Method 1
#!/bin/bash
JTP () {
echo 'Welcome to Javatpoint.'
}
JTP
Example: Method 2
#!/bin/bash
function JTP {
echo 'Welcome to Javatpoint.'
}
JTP
Passing Arguments
Like most of the programming languages, we can also pass the arguments and process the data in bash
functions. We can insert the data to the function in a similar way as passing-command line arguments
to a bash script.
To pass any number of arguments to the bash function, we are required to insert them just after the
function's name. We must apply spaces between function name and arguments. It will also be a great
choice to use double quotes around the arguments to prevent misparsing of the arguments with spaces
in it.
Following are some key points about passing arguments to the bash functions:
• The given arguments are accessed as $1, $2, $3 ... $n, corresponding to the position of the
arguments after the function's name.
• The $0 variable is kept reserved for the function's name.
Bash Functions
Functions in bash scripting are a great option to reuse code. A Bash function can be defined as a set of
commands which can be called several times within bash script. The purpose of function in bash is to
help you make your scripts more readable and avoid writing the same code again and again. It also
allows the developers to break a complicated and lengthy code to small parts which can be called
whenever required. Functions can be called anytime and repeatedly, which will enable us to reuse,
optimize, and minimize the code.
Following are some key points about bash functions:
• A function has to be declared in the shell script before we can use it.
• Arguments can be passed to the functions and accessed inside the function as $1, $2, etc.
• Local variables can be assigned within the function, and the scope of such variables will only be
that particular function.
• Built-in commands of Bash shell can be overridden using functions.
Syntax
The syntax for declaring a bash function can be defined in two formats:
1. The first method starts with the function name, followed by parentheses. It is the most preferred and
commonly used method:
function_name () {
commands
}
Single line version can be mentioned as below:
function_name () { commands; }
2. The second method starts with the function reserved word, followed by the function name:
function function_name {
commands
}
Single line version can be mentioned as below:
, function function_name { commands; }
Compared to most of the programming languages, Bash functions are somewhat limited. Let?s
understand the concept with the help of some examples:
Example: Method 1
#!/bin/bash
JTP () {
echo 'Welcome to Javatpoint.'
}
JTP
Example: Method 2
#!/bin/bash
function JTP {
echo 'Welcome to Javatpoint.'
}
JTP
Passing Arguments
Like most of the programming languages, we can also pass the arguments and process the data in bash
functions. We can insert the data to the function in a similar way as passing-command line arguments
to a bash script.
To pass any number of arguments to the bash function, we are required to insert them just after the
function's name. We must apply spaces between function name and arguments. It will also be a great
choice to use double quotes around the arguments to prevent misparsing of the arguments with spaces
in it.
Following are some key points about passing arguments to the bash functions:
• The given arguments are accessed as $1, $2, $3 ... $n, corresponding to the position of the
arguments after the function's name.
• The $0 variable is kept reserved for the function's name.