I need to get script arguments within function with arguments.
./myscript 1 2 3
function_name () {
if [ $1 == 3 ]; then
# I need the $1 in the following echo to be the script argument, not the function argument
echo $1 $2 $3
fi
}
function_name $#
Best Answer
That's quite interesting. The problem is the scope of the function is overriding that of the script. I can't see a simple way around that.
However, you can just pass the variables along as arguments (into the function):
Or you could:
$# $@
, check$1
, andecho $2 $3 $4
$# $@
, check$1
,shift
and thenecho $1 $2 $3
$@
as a script-level variable and access it from within the function