[Top][All Lists]

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

Re: [Help-bash] View Positional Parameters of Commands

From: Stephane Chazelas
Subject: Re: [Help-bash] View Positional Parameters of Commands
Date: Wed, 29 Jul 2015 12:53:20 +0100
User-agent: Mutt/1.5.21 (2010-09-15)

2015-07-28 17:50:35 -0700, Michael Convey:
> $ foo() { echo $0 $1 $2 $3 $4; }
> $ declare -F
> declare -f foo
> $ foo -a
> bash -a
> $ foo -a -b -c
> bash -a -b -c
> $
> Why is the output of $0 'bash' instead of 'foo'?

Probably by choice, and for POSIX conformance (itself probably
for Bourne compatibility that behaved like that in that function
syntax that was added to it in the 80s).

While have the function name in $0 would be more logical and
consistent, having $0 being the name of the function is not that
useful, as you know what that is when you write your script
(contrary to the script case where $0 will vary with how the
file is named or by which path the script is invoked).

While it is useful to know the name of the script in a function,
for instance in:

usage() {
  printf >&2 '%s\n' "Usage: $0..."
  exit 1

bash gives you the function name in ${FUNCNAME[0]}.

Now, some other shells do it differently. In zsh, whether $0
contains the function name (or sourced file name) depends on the
"functionargzero" option (on by default, off in sh/ksh

In AT&T ksh, functions defined with the Bourne syntax (fname()
command) don't have the function name in $0, while those defined
the Korn way (which btw predates the Bourne syntax: function
fname { body; }) have the function name in $0.

On the non-Bourne front, rc, es, akanga all have the function
name in $0. I don't thing fish can give you that information
(only the related status -f output AFAICT).


reply via email to

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