bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: proposed BASH_SOURCE_PATH


From: Léa Gris
Subject: Re: proposed BASH_SOURCE_PATH
Date: Tue, 14 May 2024 11:16:37 +0200
User-agent: Betterbird (Linux)

Le 14/05/2024 à 08:08, Martin D Kealey écrivait :
I wholeheartedly support the introduction of BASH_SOURCE_PATH, but I would
like to suggest three tweaks to its semantics.

A common pattern is to unpack a script with its associated library & config
files into a new directory, which then leaves a problem locating the
library files whose paths are only known relative to $0 (or
${BASH_SOURCE[0]}).

This needs to resolve symbolic links otherwise path will be that of the symbolic link rather than that of the actual script file:

mkdir ./tests/
cat > ./tests/real_bash_source_dir_test <<'BASH'
#!/usr/bin/env bash
real_source=$(realpath "${BASH_SOURCE[0]}")
real_source_dir=${real_source%/*}
printf 'Real Bash source dir: %s\n' "$real_source_dir"
BASH
chmod +x ./tests/real_bash_source_dir_test
ln -sr ./tests/real_bash_source_dir_test ./


Then you get same answer in both cases:
./tests/real_bash_source_dir_test
./real_bash_source_dir_test

--
Léa Gris

reply via email to

[Prev in Thread] Current Thread [Next in Thread]