In bash, when you source a file with dot:
. /some/script
you can pass positional arguments to that script:
. /some/script hello world
and then access them in the script with $1, $2, ... etc as usual. However, if they are omitted the script gets the positional arguments of the enclosing script, that is, if you have:
#!/bin/sh
#--------------script1
. ./script3 hello world
#!/bin/sh
#--------------script2
. ./script3
#--------------script3
echo $1 $2
and then do:
./script1
./script2 bonjour monde
Your results are:
hello world
bonjour monde
So, is there any way as the author of script3 to tell if your positional arguments were passed explicitly to you (as script1 does) or just got copied from the outer script (as in the script2 example)?
[EDIT] Per the suggestion in the comments, I added printing of the values in the BASH_ARGV array so script3 is now:
echo ===========script3======
echo dollar-number args: 0=$0, 1=$1, 2=$2
for i in ${!BASH_ARGV[@]} ; do
echo BASH_ARGV[$i]: ${BASH_ARGV[$i]}
done
echo ""
But the results are confusing:
./script1
script1 calling script3 with args: hello world
===========script3======
dollar-number args: 0=./script1, 1=hello, 2=world
./script2
script2 calling script3 with no args:
===========script3======
dollar-number args: 0=./script2, 1=, 2=
BASH_ARGV[0]: ./script3
./script1 bonjour monde
script1 calling script3 with args: hello world
===========script3======
dollar-number args: 0=./script1, 1=hello, 2=world
BASH_ARGV[0]: monde
BASH_ARGV[1]: bonjour
./script2 bonjour monde
script2 calling script3 with no args:
===========script3======
dollar-number args: 0=./script2, 1=bonjour, 2=monde
BASH_ARGV[0]: ./script3
BASH_ARGV[1]: monde
BASH_ARGV[2]: bonjour
Why is BASH_ARGV[0] sometimes the script name and sometimes missing and sometimes the last argument? Also the arguments are in reverse order? What a mess.