Last words

What remains to be done?

In a reasonable world: merge the two questions.

In an utopia world: Join all answers in only one question, and select the best.

But not, we are not in that utopia world, not even in that reasonable world.
We are in stackexchange.


Both answers have been modified. Probably better now. A lot of misleading comments have been removed (well, some, others were put aside). But getting here from there included an edit war, a lot of commenting (a lot of which got erased by high rep users), Many interventions of the mods, strong arm twisting and a discussion on the chat.

All of that would have been avoided if the better (newer) question and answer had been selected as the one to keep, simple.

Is that fair with the questioner and the answer ? Of course it isn't.


This question:

A perfectly valid question, well written and specific to the issue to solve, got classified as a duplicate of:

Which, on a first look seems like an (also) reasonable question of a quite similar concern.

But, looking a bit deeper, it happens that both answers in that second question have troubling (and incorrect) issues and very conflicting comments about the order of evaluation of the ++:

  • The first answer (Archemar) claims that the negation ! is applied first and then the ++.
  • Gilles claims that that error is actually correct:

the incrementation is indeed applied after the value of the expression is calculated

  • G-Man reinforces that interpretation under the cuonglm answer (the second answer).

the ! operator is applied to the value of a[$0] before it is incremented

  • And Gilles re-iterate the same in plain words:

This answer is wrong. The incrementation happens after the result of the ! operator is calculated.

At least, without taking any side, both answers result in confusion and noise.

Nobody has taken the time to address the problem and to make the answers clear, but yet, we are directing new questions to such conflicting answers.

Full disclosure: I do have a position. I believe that both G-man and Gilles are wrong. The only possible (valid) description is that the operator ++ is (as it must) be applied first to a lvalue and, ... later ... the ! operator is applied.

But the real issue to discuss here is: should we naively direct people to older answers that look (on first sight) the same but are fundamentally conflicting and confusing?

Note: this question has been written looking at what those answers look like NOW, they will probably be edited and changed. I, for one, will add another answer to try and address the (several) errors. Of course, after some time has passed to discuss this question.


I hold that we should keep to higher standards than ("first look" or "apparently similar").

What do we think?

  • in !i++, the increment of i happens after the value !i has been used, so Gilles et al is correct, the increment of the value in the variable happens after the application of !. You can test this with awk 'BEGIN { i=0; print !i++; print i }' which prints 1 and 1. The first 1 is from printing !0 and the second is from printing the incremented value (i is incremented after the value is used to calculate !i).
    – Kusalananda Mod
    Jul 11, 2020 at 7:31
  • Read and understand. There is no way in which (!i)++ could be a valid operation (in any language). So, the ++ must be applied first: !(i++). @Kusalananda
    – user232326
    Jul 11, 2020 at 7:56
  • The correct description must be: The value in a memory location (of the variable being used (or the lvalue used)) is incremented first as the ++ operator is applied (both in pre and post increments). The value used to carry on the rest of operations (the rvalue that results from the application of ++) is different in pre-increment and post-increment. In post (the example in the question) the value used for the rest of the expression is the value before the increment. But in any case, the ++ operator must be applied first. The ++ operator has two outputs !!. @Kusalananda No comment?
    – user232326
    Jul 11, 2020 at 8:10
  • You are correct that the post-increment operator has "two outputs". Can you suggest edits to the relevant answers that, in your view, clarifies them?
    – Kusalananda Mod
    Jul 11, 2020 at 8:14
  • I deleted the comment that you just replied to because I misunderstood what you meant by "applied first". This was before you added your longer explanation. We simply talked about different things, i.e. you talked about one of the outputs of ++, I thought you meant the other.
    – Kusalananda Mod
    Jul 11, 2020 at 8:15
  • You are jumping to conclusions: the end result of the operation. The steps to make that expression reach one value or other is what is being asked and what should be clarified. Yes, I have planned to write an answer, but that is not what is being asked here.
    – user232326
    Jul 11, 2020 at 8:16
  • 1
    I just wanted to what you thought was the technical issues with the answers. Without clarifying what the technical issue is and how it should be corrected, it's difficult to suggest what should be done. Here, it seems it's simply a matter of clarifying in which order the operations in !a[$0]++ happens and what values are affected by each. Doing this without adding to the confusion would be good. Adding yet another view in another answer my be confusing, so I would suggest editing the given answer.
    – Kusalananda Mod
    Jul 11, 2020 at 8:22
  • @Kusalananda I believe that I did a pretty good job both in my linked answer and in the limited space of a comment of explaining the technical issue of dealing with the two outputs of ++. There is no way in which you could understand that ++ has a lower precedence than !. What gets very confusing is that everyone choose one output of ++ and defend his/her position of having chosen the correct one to explain the end result. Again, the operator order is set (in stone) in the operator precedence table. The how that leads to the final result is what needs clarifying (both answers).
    – user232326
    Jul 11, 2020 at 8:31
  • But the question here is: why do we choose to retain a conflicting answer over a new (maybe better) one. @Kusalananda
    – user232326
    Jul 11, 2020 at 8:32
  • @Kusalananda Done both of your requested actions. Read Answer.
    – user232326
    Jul 11, 2020 at 13:06
  • 2
    @Isaac - To your broader Q I think that if a better Q & A appear, we should always close inferior Q/A towards them. It happens to work out that length of time on site may seem to skew to them always getting favoritism but keep in mind that older Q/A have been, in theory, more scrutinized vs. something that's newer.
    – slm Mod
    Jul 11, 2020 at 13:33
  • 1
    Thanks for answering the core Q. @slm (yes, in theory, theory and practice are exactly equal, in practice, the rarely are.). I reviewed both answers, we (I hope) may get better reference(s) out of the review.
    – user232326
    Jul 11, 2020 at 13:37
  • But, in the end, nobody upvoted this Q. So, is it an irrelevant question? Should it be erased? ( My personal opinion is that it shouldn't be erased, but how much my opinion really counts on this site ? ).
    – user232326
    Jul 11, 2020 at 13:47

4 Answers 4


To answer the main question: the age of a post is irrelevant. We always want to use the best post (the one with the most clearly written question, best explained answers etc.) as the dupe target. We shouldn't care which one is older, we simply want the best. That doesn't mean we will always get it right, of course, and this sort of post on meta is always welcome when you find something you think hasn't been handled in the best way by the community.

So no, we don't keep older questions by default. If a new question is better written and/or gets better answers, then that should become the new canonical and we can close older ones as dupes of the new one.

In this case, given that the two questions are indeed asking the same thing, the closure seems reasonable to me. If you feel that the existing answers in the dupe are lacking, then the best course of action is to post a new, better answer on that question.

As for the specific issues you raise, I think we're getting lost in the minor details. But then, I am a biologist, not a programmer, and I don't claim to understand the internals of how any programming language works. So I don't doubt that there is indeed a valid technical point being raised here. However, I would argue that in the context of a site about Unix and Linux, one where most of us are closer to "sysadmin" than "computer scientist", the internals might be less important than the effect.

The effect of !a[$0]++, is that it will evaluate to false if $0 has not been seen before. So no matter which operation is applied first behind the scenes, and although ++ has two outputs and not one, the final result is that it acts as though the increment occurs after the ! is evaluated. So from the point of view of someone who just needs to understand whether !a[$0]++ will return false or true the first time this value of $0 is seen, the ++ is evaluated after the !. I don't doubt that you are right and internally, the ++ is applied first but the ++ returns the old value, but I would argue that isn't really relevant to the user of the language and only important if someone is trying to understand the internals of the awk interpreter.

All this to say that I don't actually think that the two answers are contradicting each other. They both say that the expression !a[$0]++ will result in awk printing the current line if it hasn't been seen before, and that is the core question. Yes, cuonglm's answer goes into more detail and explains the internals, but the essence of the two answers is the same: this expression will print the current line if it hasn't been seen before.

  • Yes, we probably: getting lost in the minor details And, you also tripped on the same wire. The detail discussion of what is first? The egg or the chicken was raised by Archemar, cuonglm, Gilles and G-Man a very long time ago. And that you fall into discussing it again just goes to prove that the discussion is distracting, noisy, confrontational and just a flame war. It is therefore better to keep the newer question, in which no-one miss the core issue, all provide useful points of view and answers the same questions without noise. .... See? you went with the older, and you shouldn't. :-
    – user232326
    Jul 11, 2020 at 15:01
  • I already provided a new answer Maybe it will be helpful if you read it.
    – user232326
    Jul 11, 2020 at 15:05
  • Let me try a biology simile to the detail we are discussing (not that I know the least bit about biology): It doesn't mater if there are four or six bases on the ADN, what it matters is that the accumulated knowledge of each specie is encoded in those 6 bases adgcft. .... do you think that that is an important detail? It doesn't change the final description: it encodes the way an specie is built, doesn't it?
    – user232326
    Jul 11, 2020 at 15:37

Should the new question be closed as a duplicate?

This feels wrong to me.

The duplicate is not necessary to provide an answer where we didn't already have one. ilkkachu wrote a perfectly great answer to the new question.

The "duplicate" is different: the new post is a more narrow question. I thought we liked questions that we can answer very specifically :-).

As Isaac pointed out, the result looks really strange. The accepted answer on the old page does not attempt to address the narrow question. Because the old question does not explicitly ask the narrow question. In fact, reading the old answer tends to confuse and suggest the opposite of the correct answer to the narrow question.

extended ranting which is not relevant to the above

I am frustrated by the older page. StackExchange tradition argues this can be addressed by a couple of approaches.

Problem 1: one considers the accepted answer to be "wrong"

To me: it is a question about a programming language. (One which is on-topic for Unix.SE). The expression !x++ is evaluated as !(x++). To answer the old question "exactly", as it requests, this can be broken down into x++ first, then the ! operator. It is a mistake to break it down into !x, followed by the ++ operator.

-> What should be done with accepted, yet wrong, answers?

One of the approaches is "provide a correct answer [...] You can also add a comment to point out what's wrong with the answer."

Sometimes the accepted answer will remain unchanged. This is a longstanding annoyance (search "accepted wrong answer" on Meta.SE). But we don't want mods to work around it and judge lots of complex cases. I don't want mods to have to delete accepted answers or allow special edits, unless there is quite an obvious justification (example). You could set more site-specific standards. But I'd like moderators who manage people problems, not who pretend they know everything about Unix.

Problem 2: comment chaos

Tradition says "comments are not for extended discussion". If a comment has not convinced the answerer to edit, the solution is to write an alternative answer. (In this case, this has already been done).

When necessary, use a short comment to criticize the answer. It would be most convenient to link to the alternative answer. Though if I see a clear critique, I'm probably going to scroll down to look for the alternatives anyway.

The traditional answer to comment chaos is "comments are transient".

For example, shouldn't we lean towards cleaning up by removing the following comment? Since @Gnouc deleted their comment, it is hard to work out what the reply to @Gnouc means :-). I.e. nominate the comment for deletion, by flagging it as "no longer needed" ("this comment is outdated"). If the comment is deleted, it will be easier to read and understand the comments. The critique will be easier to see, so it will be clearer that one should look at the alternative answers.

@Gnouc I don't see any serious error in this answer. If that is what you're referring to, the incrementation is indeed applied after the value of the expression is calculated. It's true that the incrementation happens before the printing, but that's a minor imprecision which doesn't affect the basic explanation.

Does that feel like exploiting the system? I concede it devalues the comment-writer and their effort... but isn't the system supposed to hold comments as lower value? and this comment-writer is surely experienced enough to expect it...


Even though it was done in the question above, I'm not really one of those beings that appear when you chant their name three times in a row...

But I also did get this meta post linked to me, so I took a look at the posts it was about, and ended up editing two answers from the 2014 Q -- the top two ones. One of them seemed ok, but I felt it could be phrased a bit more clearly. I don't know if I succeeded in that, feel free to revert if you like.

As for the other, the edit wasn't that light, and I was also indirectly called for it, so perhaps I should answer for that.

The way the answer stood when I saw it, after all the edits made to it lately, it honestly was just confusing to read. In particular, the last two lines put two very different things side-to-side as equals: the printing of the line, which is conditional on the value of the expression; and the effect of the post-increment, which is not conditional.

An earlier version of the answer seemed better in that it made it clearer that only the printing is conditional on the prior status of a[$0]. Though even here, what happens when the next identical line comes is only brushed upon. That, I think is a rather important part of how the program works, and the question also did ask "how exactly this notation is interpreted", which does all for some detail.

(Though to be honest, the question is somewhat difficult to answer. In the title it just asks how the program works; and later in the post itself, it mentions that the basic idea is already known, but question is in fact about details...)

Now, I do admit that my edit ended up rather more invasive than the intention was. But faced with a +40 accepted answer, the alternative of just downvoting and walking away didn't seem very useful for any future readers either.

  • Since you dislike that your answer (not you) has been named three times, I erased all reference to them.
    – user232326
    Jul 15, 2020 at 16:00
  • after all the edits made to it lately The only edit that anyone (except the owner of the answer) did before yours was done by me and reverted almost immediately. The answer stood in its incomplete/half baked state for several years. Add conflicting comments. So, you got lucky in that nobody reverted your edits. After a lot of persistence, I believe we got better answers. Note that upvotes were divided between both answers, that is an usual indication that readers are undecided about which is better, that is usually an indication of poor/conflicting answers. That is better now, I think.
    – user232326
    Jul 15, 2020 at 16:17
  • (-1) In that this (your) answer doesn't answer the question, doesn't even try, but is more alike to an extended comment to explain some personal feelings I am forced to downvote it.
    – user232326
    Jul 15, 2020 at 16:20
  • @Isaac, well, I didn't say whose edits they were, just that in my opinion, it was specifically the recent edits that made it worse. And yes, you're quite right, this wasn't even meant as an answer to the question of what to do with old posts in general. I started writing this as an answer to this but it didn't fit in a comment, even in a briefer form.
    – ilkkachu
    Jul 15, 2020 at 18:39

I "fixed" my answer on old question about a[$0] not "defined".my answer's got a life of its own now.

about: ++

  • As I mention in chat, one shouldn't use a+=a++*a++ and expecting to know result.
  • ++ is to be used in a = foo + i++ or a = foo[i++] like operations, whatever language, unless you are competiting in codegolf.
  • last I had to implement ++ operator was at university, my work is gone with my computer of that time (that was about 2**5 years ago). To be hosnest I didn't remember any form of "forking" (setting value and evaluation of expression).

about answers

  • at the time of first post, I was merely happy to explain this little trick, altough I was confused by Cuonglm's comment and Cuonglm's answer.

  • (nowdays) I was about to came here and asked my answer to be deleted, however not all user came here with perfect knowledge on how does ++ compile in pseudo-code.

  • I am sorry to have offended thoses who knows.

  • I don't believe that you have offended anyone, well, not me at least. Others were more difficult.
    – user232326
    Jul 13, 2020 at 16:30
  • I was about to write something here, but it was too long for a comment even in the briefer form, so posted a longer answer instead.
    – ilkkachu
    Jul 15, 2020 at 0:05

You must log in to answer this question.