I think the most robust method is by using the appropriate bash variables. This avoid confusion between
executed and
sourced scripts. There is also a specific variable to retrieve function names:
Code:
#!/bin/bash
#
test () {
echo $FUNCNAME
echo $1
echo $2
}
test 1 2
echo $(readlink -f "$BASH_SOURCE")
The last statement gives the full path of the script:
Code:
$ ./test.sh
test
1
2
/home/colucix/test.sh
$ . test.sh
test
1
2
/home/colucix/test.sh
$ bash test.sh
test
1
2
/home/colucix/test.sh
Hope this helps.