variable scope

Hi,
I want to know about the variable scope in shell script.

How can we use the script argument inside the function?

fn () {
echo $1   ## I want this argument should be the main script argument and not the funtion argument.
}

also are there any local,global types in shell script?
if yes, how they are handled?

Thanks in advance.

You can always pass all the script arguments to a function..

fn "$@"

By default, variables are global in the subshell in which they are created.
If you want to define a variable in a function and don't want it to be visible outside it, use the "local" bultin command.

If you need both, then you must copy arguments ex. to array.

#!/bin/ksh
# arg
n=1
while [ $# -gt 0 ]
do
   args[$n]=$1
   shift
   (( n+=1 ))
done

fn()
{
    echo "1:${args[1]}"
    echo "3:${args[3]}"
    echo "4:${args[4]}"
    echo "my1:$1"
}

fn hello

test:
chmod a+rx arg
./arg 121 3243 "kkk jjj" lll

Yes, and you can initialize the array from positional parameters even more elegantly:

args=("$@")