Skip to content

Instantly share code, notes, and snippets.

@toshi75
Created August 11, 2023 13:26
Show Gist options
  • Save toshi75/bf9a8741473f5e05436aa3c6b5bd0691 to your computer and use it in GitHub Desktop.
Save toshi75/bf9a8741473f5e05436aa3c6b5bd0691 to your computer and use it in GitHub Desktop.
$0 and $BASH_SOURCE #shell
func_test (){
# echo file name of "test.scr"
cat << '____SCR' > test.scr
echo $0
echo $BASH_SOURCE
echo ${BASH_SOURCE:-$0}
____SCR
chmod 777 ./test.scr
local N=1
echo '## ./test.scr #################'
./test.scr
echo
for i in {bash,source,zsh} ;do
case $N in
1) echo '## bash ./test.scr ############' ;;
2) echo '## source ./test.scr ##########' ;;
3) echo '## zsh ./test.scr #############' ;;
esac
$i ./test.scr
echo
let N++
done
echo '## END ########################'
rm ./test.scr
return 0
}
: << '____RESULT'
## ./test.scr #################
./test.scr
./test.scr
./test.scr
## bash ./test.scr ############
./test.scr
./test.scr
./test.scr
## source ./test.scr ##########
-/bin/bash
./test.scr
./test.scr
## zsh ./test.scr #############
./test.scr
./test.scr
## zsh ./test.scr #############
____RESULT
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment