View Issue Details

IDProjectCategoryView StatusLast Update
0001478Issue 8 draftsShell and Utilitiespublic2024-06-11 09:12
Reporterstephane Assigned To 
PrioritynormalSeverityEditorialTypeClarification Requested
Status ClosedResolutionAccepted As Marked 
Product VersionDraft 2 
NameStéphane Chazelas
Organization
User Reference
Section2.6.2 Parameter Expansion
Page Number2318-2320
Line Number
Final Accepted Text0001478:0005389
Summary0001478: clarify ${@?error}, ${@+set}, "${@+set}", "${*+set}"... etc. expansions
DescriptionWhile the spec does leave expansions of ${@#pattern} (and variants with ##, %, %%) unspecified, or that $@/$* expansion shall not cause the shell to exit when the nounset option is set (without going as far as saying that $@ / $* should be considered always set), it leaves it quite unclear what the expansion of

${@?error} "${@?error}", ${@+"$@" more}, "${*-foo}", "${@:+foo}", ${*+foo} should yield.

In practice, there is a lot of variation in behaviour between implementations, with some implementations considering $@/$* "set" always (like for set -u) or only if $# > 0. Or "$*" "non-empty" only if $1 is non empty or some only if "$*" would yield a non-empty string (with variations in behaviour if $IFS is empty), etc. For some ${@?error} is a syntax error, etc.

Some shells with array support try to be consistent with their handling of $@ compared to that of other arrays, but in different ways as their arrays take different shapes, etc.

The only portable variants I've managed to find were: "${*-foo}" and "${*+foo}" which expand to foo (resp. "$*") if and only if $# == 0, which is not among the most useful ones.
Desired ActionLeaving the expansion of all those - + ? :- :+ :? operators unspecified for the @ and * parameters seems like the least bad option.

Alternatives are likely to either make things more confusing or break some shells backward compatibility.

Users can always spell things out like:

if [ "$#" -eq 0 ]; then
  ...
else
  ...
fi


or if:

if [ -n "$*" ]; then
  ...
else
  ...
fi


Tagsissue8

Activities

stephane

2021-05-23 16:16

reporter   bugnote:0005365

For the record, this came to my attention at

https://unix.stackexchange.com/questions/650208/issue-processing-multiple-files-counting-lines/650590#comment1220610_650590

Where someone posted this:

#!/bin/sh -

wc -l -- "${@?expected to get filename argument(s)}"


code which in practice is not portable, and for which I'm not sure what the POSIX spec currently says.

geoffclare

2021-06-21 09:37

manager   bugnote:0005389

Last edited: 2021-06-21 09:38

Suggested change:

On page 2318 line 74742 section 2.6.2 append a sentence to the paragraph:
If parameter is '*' or '@', the result of the expansion is unspecified.


Issue History

Date Modified Username Field Change
2021-05-23 16:12 stephane New Issue
2021-05-23 16:12 stephane Name => Stéphane Chazelas
2021-05-23 16:12 stephane Section => 2.6.2 Parameter Expansion
2021-05-23 16:12 stephane Page Number => 2318-2320
2021-05-23 16:16 stephane Note Added: 0005365
2021-06-21 09:37 geoffclare Note Added: 0005389
2021-06-21 09:38 geoffclare Note Edited: 0005389
2021-07-01 16:09 geoffclare Final Accepted Text => 0001478:0005389
2021-07-01 16:09 geoffclare Status New => Resolved
2021-07-01 16:09 geoffclare Resolution Open => Accepted As Marked
2021-07-01 16:09 geoffclare Tag Attached: issue8
2021-07-20 09:33 geoffclare Status Resolved => Applied
2024-06-11 09:12 agadmin Status Applied => Closed