Writing a Nested shell script- Unable to pass argument
Your "outer" shell script is interpreting $i as if it were one of its own variables, which isn't set, thus it evaluates to nothing. Try escaping the $ so the outer shell doesn't expand it:
echo \$i
here is the "foreach" function:
function foreach{ typeset cmd=$1; shift; for arg in "$@"; do $cmd $arg; done}