I want to put all common functions in a "included_by_other_shell_script.sh
" and then call it by using source
command.
Here is the sourcing
script: /tmp/main/sourcing_test.sh
#!/bin/bash
source /tmp/sourced/included_by_other_shell_script.sh
echo "test_dir: $test_dir"
Here is the sourced
script: /tmp/sourced/included_by_other_shell_script.sh
#!/bin/bash
get_bash_script_dir() {
printf "%s" "${BASH_SOURCE[0]}"
}
test_dir="$(get_bash_script_dir)"
Run the sourcing test:
/tmp/main/sourcing_test.sh
Here is the output:
root@test:~# /tmp/main/sourcing_test.sh
test_dir: /tmp/sourced/included_by_other_shell_script.sh
Here is the expected output:
root@test:~# /tmp/main/sourcing_test.sh
test_dir: /tmp/main
How to get the sourcing bash dir in the common function "get_bash_script_dir()
"?