How to pass parameters to function in a bash script?

To call a function with arguments:

function_name "$arg1" "$arg2"

The function refers to passed arguments by their position (not by name), that is $1, $2, and so forth. $0 is the name of the script itself.

Example:

#!/bin/bash

add() {
    result=$(($1 + $2))
    echo "Result is: $result"
}

add 1 2

Output

./script.sh
 Result is: 3

In the main script $1, $2, is representing the variables as you already know. In the subscripts or functions, the $1 and $2 will represent the parameters, passed to the functions, as internal (local) variables for this subscripts.

#!/bin/bash
#myscript.sh
var1=$1
var2=$2
var3=$3
var4=$4

add(){
  #Note the $1 and $2 variables here are not the same of the
  #main script... 
  echo "The first argument to this function is $1"
  echo "The second argument to this function is $2"
  result=$(($1+$2))
  echo $result

}

add $var1 $var2
add $var3 $var4
# end of the script


./myscript.sh 1 2 3 4