Yes, that's precisely my point. It suddenly becomes more complex and bug prone than at first glance. To do it without a subshell, I need something like this boilerplate at the top of each of my main scripts:
if [[ "${BASH_SOURCE[0]}" == */* ]]; then SCRIPT_DIR="${BASH_SOURCE[0]%/*}" else SCRIPT_DIR="." fi -Will On Wednesday, June 19, 2024 at 02:08:27 PM PDT, Koichi Murase <myoga.mur...@gmail.com> wrote: 2024年6月20日(木) 5:47 Léa Gris <lea.g...@noiraude.net>: > Look like you did not find a proper answer there. Here is one simple > that involve no sub-shell at all and does exactly what your sub-shell > version does. > > declare -r SCRIPT_DIR=${BASH_SOURCE[0]%/*} This doesn't work as explained by Will. BASH_SOURCE doesn't contain a slash when the script is specified as e.g. `bash a.sh' or `source a.sh'. > [...] it is not system agnostic anymore. POSIX 2024 containing realpath(1) was just published, though I'm not sure when/whether it will be available in very old systems. -- Koichi