In this recipe, we are going to see functions in shell scripts. We will see how our program testing is done, sequentially on the various parts, using functions. Functions help improve the readability of a program.
We are going to write a simple function in our shell script to return the current date and time. Create a script function_example.sh
, and write this code in it:
#!/bin/bash print_date() { echo "Today is `date`" return } print_date
Now we will create another script containing two functions with the same name. Create a script function2.sh
, and write the following content in it.
#!/bin/bash display ( ) { echo 'First Block' echo 'Number 1' } display ( ) { echo 'Second Block' echo 'Number 2' } display exit 0