bug-bash
[Top][All Lists]
Advanced

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

Re: Bash 5.1: Make shell_script_filename available to startup files


From: Chet Ramey
Subject: Re: Bash 5.1: Make shell_script_filename available to startup files
Date: Tue, 20 Feb 2024 11:17:14 -0500
User-agent: Mozilla Thunderbird

On 2/17/24 6:47 PM, Zachary Santer wrote:

If you're satisfied with $_ or now $0, then fine, but I would actually
expect that to show up as ${BASH_SOURCE[1]} within the ${BASH_ENV} script,
which it obviously doesn't. Don't know what ${BASH_LINENO[0]} ought to be
in that case. Maybe 0?

The way the manual explains BASH_SOURCE and BASH_LINENO is all in
reference to FUNCNAME, which doesn't exist (or is empty) if you're not
executing a function.

BASH_SOURCE and BASH_LINENO are part of the debugger support, and exist so
the debugger can create a function call stack.

Bash pushes the name of a script it's executing to provide a base for the
call stack, but only after it successfully opens it and is ready to read
and execute commands (with the dummy funcname of "main").

`source' changes BASH_SOURCE so you can figure out where functions are
defined and perhaps executed.

Lots of folks define functions they want to use in their startup files,
so executing startup files like .bashrc also modifies BASH_SOURCE to say
which file is currently being read. BASH_ENV works the same way: reading
commands from it puts the name of the file where functions are presumably
being defined and maybe executed into BASH_SOURCE.

Since startup files are read before any commands, interactive or non-
interactive, the name of any shell script isn't in BASH_SOURCE when reading
the startup files.

--
``The lyf so short, the craft so long to lerne.'' - Chaucer
                 ``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, UTech, CWRU    chet@case.edu    http://tiswww.cwru.edu/~chet/




reply via email to

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