[Toybox] Would someone please explain what bash is doing here?

Chet Ramey chet.ramey at case.edu
Mon May 4 11:16:32 PDT 2020


On 5/4/20 12:20 PM, Rob Landley wrote:
> On 5/4/20 8:39 AM, Chet Ramey wrote:
>> On 5/2/20 4:01 PM, Rob Landley wrote:
>>
>>> Why does  the export flag on $_ toggle?
>>
>> It does three different things. It's initially exported if bash inherits it
>> from the environment at startup; after that it does its thing.
>>
>> "_      At shell startup, set to the absolute pathname  used  to  invoke
>>         the  shell or shell script being executed as passed in the envi-
>>         ronment or argument list.  Subsequently, expands to the last ar-
>>         gument  to  the  previous  simple  command executed in the fore-
>>         ground, after expansion.  Also set to the full pathname used  to
>>         invoke  each  command executed and placed in the environment ex-
>>         ported to that command.   When  checking  mail,  this  parameter
>>         holds the name of the mail file currently being checked."
>>
>> We picked that up from ksh.
> 
> My question was why "declare -p _" was showing -x sometimes and not other times,
> but I guess that's just an implementation detail showing through? 

Yeah, pretty much. It should show as exported all the time, since it was
imported from the initial environment, but the code that sets it to the
last argument of the previous simple command makes sure it doesn't have
the export attribute.


>(Unlike other
> magic variables, if you unset _ it doesn't stop being magic, it still gets reset
> every command. 

It predates dynamic variables by several years.

> And if you "readonly _" bash gets REALLY chatty. :)

That falls into the category of giving you enough rope to shoot yourself.

> Still trying to work out what the "bash spec" would be, vs implementation details...

I'll be interested when you get that spec done.


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



More information about the Toybox mailing list