bug-bash
[Top][All Lists]
Advanced

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

Re: 'exec -a' and $0 substitution


From: Sergei Trofimovich
Subject: Re: 'exec -a' and $0 substitution
Date: Thu, 26 Jan 2023 08:00:09 +0000

On Thu, 26 Jan 2023 00:35:27 -0500
Lawrence Velázquez <vq@larryv.me> wrote:

> On Wed, Jan 25, 2023, at 6:37 PM, Sergei Trofimovich wrote:
> > I fear it's a side-effect of the way 'bash' gets executed via shebang by
> > kernel. But maybe not? Somehow direct script execution still manages to
> > preserve script's name. Is it an intended behaviour that could not be
> > easily changed? Or it's a bug?  
> 
> It's not a bug.

Thank you! That will make generic wrapping a bit more involved for us.

> https://www.gnu.org/software/bash/manual/html_node/Special-Parameters.html#index-0
> 
>       0
>               ($0)  Expands to the name of the shell or shell
>               script.  This is set at shell initialization.  If
>               Bash is invoked with a file of commands (see Shell
>               Scripts), $0 is set to the name of that file.  If
>               Bash is started with the -c option (see Invoking
>               Bash), then $0 is set to the first argument after
>               the string to be executed, if one is present.
>               Otherwise, it is set to the filename used to invoke
>               Bash, as given by argument zero.
> 
> Whenever bash is executed with a script file, it sets $0 to the
> name of that file.  It only uses caller-provided values for $0 when
> a script file is not provided.
> 
>       $ (exec -a foo bash <<<'echo "$0"')
>       foo
>       $ (exec -a foo bash -s bar baz quux <<<'echo "$0"')
>       foo
>       $ (exec -a foo bash -c 'echo "$0"')
>       foo
>       $ (exec -a foo bash -c 'echo "$0"' bar baz quux)
>       bar
> 
> This behavior is standardized in POSIX [*] and is not unique to
> bash (I omitted ksh93, which actually does produce "foo" here).
> 
>       $ (exec -a foo zsh <(echo 'echo "$0"'))
>       /dev/fd/63
>       $ (exec -a foo dash <(echo 'echo "$0"'))
>       /dev/fd/63
>       $ (exec -a foo yash <(echo 'echo "$0"'))
>       /dev/fd/63
>       $ (exec -a foo mksh <(echo 'echo "$0"'))
>       /dev/fd/63
> 
> [*]: 
> https://pubs.opengroup.org/onlinepubs/9699919799/utilities/sh.html#tag_20_117_05
> 
> -- 
> vq


-- 

  Sergei



reply via email to

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