Augmenting functions with parameters
Just as a script can accept input in the form of arguments, so can a function. In reality, most functions will use parameters. Static functions, such as the error handling example from earlier, are not as powerful or flexible as their counterparts to accept arguments.
Colorful
In the next example, we'll create a script that allows us to print text to our terminals in a few different colors. It does this based on a function that has two parameters: string
and color
. Take a look at the following commands:
reader@ubuntu:~/scripts/chapter_13$ vim colorful.sh reader@ubuntu:~/scripts/chapter_13$ cat colorful.sh #!/bin/bash ##################################### # Author: Sebastiaan Tammer # Version: v1.0.0 # Date: 2018-11-17 # Description: Some printed text, now with colors! # Usage: ./colorful.sh ##################################### print_colored() { # Check if the function was called with the correct arguments. if [[ $# -ne 2 ]]; then echo "print_colored...