Skip to content

Instantly share code, notes, and snippets.

@mpdaugherty
Created May 14, 2012 03:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mpdaugherty/2691574 to your computer and use it in GitHub Desktop.
Save mpdaugherty/2691574 to your computer and use it in GitHub Desktop.
A test file that illustrates the use of $BASH_SOURCE and $FUNCNAME
#!/bin/bash
function f1() {
echo 'In f1'
echo ''
echo ${BASH_SOURCE[0]} + ${FUNCNAME[0]}
echo ${BASH_SOURCE[1]} + ${FUNCNAME[1]}
}
echo 'in bash_source_test.sh'
echo ''
echo ${BASH_SOURCE[0]} + ${FUNCNAME[0]}
echo ''
mpdaugherty@grit ~ $ ./bash_source_test.sh
in bash_source_test.sh
./bash_source_test.sh +
In f1
./bash_source_test.sh + f1
./bash_source_test.sh + main
mpdaugherty@grit ~ $ source bash_source_test.sh
in bash_source_test.sh
bash_source_test.sh +
In f1
bash_source_test.sh + f1
bash_source_test.sh + source
@jfno
Copy link

jfno commented Feb 11, 2016

I needed to add a call to f1 as the last line of the script to get the same result as you show.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment