Q:

Get return value of function in a loop - shell script

Can't seem to understand the output of the following code snippet. trying to print the function return value in a loop
contains () {
 local e
 for e in "${@:2}"; do [[ "$e" == "$1" ]] && return 0; done
 return 1
}

line="ayush"
line2="this is a line containing ayush"
contains $line $line2
echo $?  #prints 0
for i in 1 2 3;do
    contains "$line" "$line2"
    echo $? #prints 1 everytime
done

A:

@Ayush Goel
The Problem is here,
contains () {
  local e
  for e in "${@:2}"; do [[ "$e" == "$1" ]] && return 0; done
  return 1
}

line="ayush"
line2="this is a line containing ayush"

contains $line $line2
echo $?  #prints 0

for i in 1 2 3;do
   contains $line $line2   # <------------------ ignore ""
   echo $? # Now it will print 0 
done

Difference between $var and "$var" :
1) $var case
var="this is the line"
for i in $var; do
    printf $i
done

here it will print 
this is the line

means $var is expanded using space
2)"$var" case
var="this is the line"
for i in "$var"; do
    printf $i
done

this will print
this

here "$var" will be considered as a single argument and it will take only one value from the list.

