2

Answering this question, I found that in:

echo 1 2 | awk '{print $(expression)}' 

the return value of expression is unspecified. It can lead to confusion, as if expression is an uninitialized variable, should $(expression) return 0 (as evaluated in numeric context) or return empty string "" ( as evaluated in string context) or raise an error.

Since when field variables were defined as:

Field variables shall be designated by a '$' followed by a number or numerical expression

Why POSIX left the return value of any expression follow $ unspecified? Is it a historical reason or something else?

3
  • Your question isn't clear. POSIX does define $(…), but only if evaluates to a nonnegative ineteger. Are you asking why the value of an expression consisting of an undefined variable is left unspecified? (That's what you seem to be asking, but then what does the $ operator have to do with it?) Commented Jan 6, 2016 at 23:16
  • 1
    @Gilles: POSIX said that Field variables shall be designated by a '$' followed by a number or numerical expression. The effect of the field number expression evaluating to anything other than a non-negative integer is unspecified; uninitialized variables or string values need not be converted to numeric values in this context. Then the expression follow $ need not be converted to numeric values, it means $(expression) unspecified, doesn't it? Commented Jan 7, 2016 at 1:34
  • @Gilles: And also, if expression was evaluated in numeric context, then the result must be a number. Here the expression follow $ wasn't defined to have numeric or string context, that's how $ operator affect. Commented Jan 7, 2016 at 1:42

1 Answer 1

2

POSIX tried hard to codify existing practice, and only broke new ground where unavoidable (yes, that is after-the-fact standards for you ;-). As Unix code was never exactly the epitome of consistency (for example, original vi(1) returned a completely random exit code, presumably to shave off a few bytes of the executable when that was critical), taken by literally hundreds of different groups, modified according to different agendas, spawned clones (like GNU, there were several others), much was left undefined to cover all sort of random behaviour.

3
  • 1
    Yes. Here Brian Kernighan's awk errors out with illegal field $(), name "expression", it's OK with $(0+expression) though (same as $0). Commented Jan 7, 2016 at 8:10
  • 1
    @StéphaneChazelas: That's also what I said in my answer here unix.stackexchange.com/q/253570/38906. I am really curious about any reason POSIX made that choice Commented Jan 7, 2016 at 8:13
  • @cuonglm, you'd have to check the minutes of the POSIX discussions (if nothing shows up in some rationale section). At least find a reasobably successful version/clone that handles this differently. Commented Jan 7, 2016 at 9:59

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.