Its pretty simple to work with functions of commandline bash/sh scripts.
Define function
func_name(){ <bash command_1> .... <bash command_n> }
say_hello(){ echo "Hello World" }
Calling the function
say_hello(){ echo "Hello World" } say_hello
Passing parameters to the function
say_hello(){ echo "Hello $1 $2" } say_hello "Saminda" "Wijeratne"
Defining variables inside a function
say_hello(){ local first_name=$1 last_name=$2 #becomes a global variable echo "Hello $first_name $middle_name $last_name" } middle_name="Saminda" #global variable say_hello "Kushan" "Wijeratne" echo $firt_name echo $last_name
say_hello(){ eval "declare -A bio_data_array="${1#*=} #now bio_data_array is the array that was passed as a parameter echo "Hello ${bio_data_array['name']}" } declare -A bio_data bio_data["name"]="Saminda" bio_data["age"]=28 say_hello "$(declare -p bio_data)"
Returning values from the function
echo "This is what the say_hello function had to say: $output_string"