[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: is there a difference between $BASH_SOURCE and $0 in portability ?
From: |
Jeffrey Walton |
Subject: |
Re: is there a difference between $BASH_SOURCE and $0 in portability ? |
Date: |
Wed, 16 Feb 2022 19:11:10 -0500 |
On Wed, Feb 16, 2022 at 5:38 PM Alex fxmbsw7 Ratchev <fxmbsw7@gmail.com> wrote:
>
> i ask cause BASH_ isnt maybe availabe, so i must use $0 to exec with new
> bash again ( os x ) .. whats the difference .. ( of $0 vs BASH_SOURCE ) ?
In practice, yes. When I test some of my scripts on really old Bash
(like Fedora 1 or Ubuntu 4), BASH_SOURCE (builtin) has different
behavior than assigning:
if [[ -z "$BASH_SOURCE" ]]; then
BASH_SOURCE=$0
fi
I tried using the assignment because some of my scripts were meant to
be sourced, and they did this:
if [ ! -e "${ANDROID_TOOLCHAIN}/$CC" ]; then
echo "ERROR: Failed to find Android clang. Please edit this script."
[ "$0" = "${BASH_SOURCE[0]}" ] && exit 1 || return 1
fi
Jeff