I have two Bash scripts in the same folder (saved somewhere by the user who downloads the entire repository):
script.sh is run by the user
$0 is considered unsafe by many devs. I have found another solution, it is safe for a chain of bash scripts and source.
If a.sh needs to execute b.sh (located in the same folder) using a child bash process:
#!/bin/bash
__dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
bash ${__dir}/b.sh
If a.sh needs to execute b.sh (located in the same folder) using the same bash process:
#!/bin/bash
__dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
source ${__dir}/b.sh