Relevant information can be found on the man page provided by the BASH maintainer (last checked August 2020). Section Shell Grammar, Simple Commands states (emphasis added):
A simple command is a sequence of optional variable assignments followed by blank-separated words and redirections, and terminated by a control operator. The first word specifies the command to be executed, and is passed as argument zero. The remaining words are passed as arguments to the invoked command.
So you can pass any variable you'd like. Your echo
example does not work because the variables are passed to the command, not set in the shell. The shell expands $x
and $y
before invoking the command. This works, for example:
$ x="once upon" y="a time" bash -c 'echo $x $y'
once upon a time
Since POSIX documentation allow it as an extension, there's nothing prevent implementation from that behavior.
A simple check (ran in zsh
):
$ for shell in /bin/*sh 'busybox sh'; do
printf '[%s]\n' $shell
$=shell -c 'á() { :; }'
done
[/bin/ash]
/bin/ash: 1: Syntax error: Bad function name
[/bin/bash]
[/bin/dash]
/bin/dash: 1: Syntax error: Bad function name
[/bin/ksh]
[/bin/lksh]
[/bin/mksh]
[/bin/pdksh]
[/bin/posh]
/bin/posh: á: invalid function name
[/bin/yash]
[/bin/zsh]
[busybox sh]
sh: syntax error: bad function name
show that bash
, zsh
, yash
, ksh93
(which ksh
linked to in my system), pdksh
and its derivation allow multi-bytes characters as function name.
yash
is designed to support multibyte characters from the beginning, so there's no surprise it worked.
The other documentation you can refer is ksh93
:
A blank is a tab or a space. An identifier is a sequence of
letters, digits, or underscores starting with a letter or
underscore. Identifiers are used as components of variable names. A vname is a sequence of one or more identifiers separated by a . and optionally preceded by a .. Vnames are used as function and variable names. A word is a sequence of
characters from the character set defined by the current locale, excluding non-quoted metacharacters.
So setting to C
locale:
$ export LC_ALL=C
$ á() { echo 1; }
ksh: á: invalid function name
make it failed.
Best Answer
The only reference I can find to the special parameter
$_
in POSIX is in the rationale section on Shell Variables. This excerpt implies that it was used by a variety of shells, but not in a standard way by all and was omitted intentionally:So it may be used by most or all POSIX-conforming shells, but it is decidedly not defined by POSIX.