Bug 255144 - /bin/sh: inconsistency between implementation and manpage
Summary: /bin/sh: inconsistency between implementation and manpage
Status: New
Alias: None
Product: Base System
Classification: Unclassified
Component: bin (show other bugs)
Version: 13.0-STABLE
Hardware: Any Any
: --- Affects Many People
Assignee: freebsd-bugs (Nobody)
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2021-04-17 08:01 UTC by Helge Oldach
Modified: 2021-04-17 14:43 UTC (History)
2 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Helge Oldach 2021-04-17 08:01:01 UTC
sh(1) states about the use of alias:

     alias [name[=string] ...]
             If name=string is specified, the shell defines the alias name
             with value string.  If just name is specified, the value of the
             alias name is printed.  With no arguments, the alias built-in
             command prints the names and values of all defined aliases (see
             unalias).  Alias values are written with appropriate quoting so
             that they are suitable for re-input to the shell.  Also see the
             Aliases subsection.

So I would expect that only the value is printed when asking for it. That does not seem to be the case. Actually also the assignment is being printed:

$ alias l
l='ls -laFG'
$

Further the output should be suitable for being fed back into the shell. That is true but actually will create a shell variable assignment in the sub-shell. That might be the intended behavour but IMHO is an odd deviation of /bin/sh's alias/variable logic:

$ alias l | sh
$

Linux bash behaves differently, it also outputs the "alias" command before printing name and value, so that this can be fed into a sub-shell as a command, effecting the same result.

bash# alias vi
alias vi='vim'
bash#

Actually what I would like to achieve is something like this:

$ alias l | batch

which would be possible if our /bin/sh would simply print the value, as claimed by the manpage. (Of course the 'l' example is not very much sensible, but think of more complex alias incantations with command sequences to be submitted into a batch run.)

Would it make sense to align our /bin/sh code with what the manpage states? The diff is trivial...
Comment 1 Ceri Davies 2021-04-17 12:09:25 UTC
The current format is defined by POSIX, I believe: https://pubs.opengroup.org/onlinepubs/9699919799/utilities/alias.html
Comment 2 Helge Oldach 2021-04-17 14:43:24 UTC
(In reply to Ceri Davies from comment #1)
True. Should the manpage be corrected then?

Maybe extend with an "alias -r" (raw) command that just prints the value without quoting? Basically the equivalent of:

rawalias() { local _x; _x=$(alias $1) && echo $(eval echo -n ${_x#$1=})
}

FWIW, our shell extends beyond POSIX, for example POSIX doesn't know Dollar-Single Quotes.