You can't pass an array as a single argument to a function. When you write "${cut_pages[@]}" it spreads the array into separate arguments to the function.
I've changed the function to take the value to search for as the first argument, and the array is all the rest of the arguments. After assigning this to value, the shift command removes it from the argument list, and then I iterate over "$@" to process the remaining arguments.
You're also not getting the result of the function correctly. $? contains the status code in the return statement. To get what the function echoes, you use $(...). I've removed echo "was found" so this won't get into the result.
#!/bin/bash
function get_index() {
value="$1"
shift
local index=0
for i in "$@"; do
if [[ "$i" = "${value}" ]]; then
echo "${index}";
return
fi
((index++))
done
return 1
}
echo "index test"
cut_pages=(2 5)
if index=$(get_index 5 "${cut_pages[@]}"); then
echo $index
fi
my_arrayis a string, not an array.$1 == ${cut_pages[0]},$2 == ${cut_pages[1]}and so on.$?contains the value used in thereturnstatement, not the output of the function. You need to useindex=$(get_index ...)to get the output.