When I open Terminal, I get an "I have no name!" prompt

Something in memory got corrupted, and the mapping between your userID (502) and your username (ipd) was lost. I've seen it happen (usually when I've manually killed hung system processes), although I'm not sure exactly what causes it. launchd maybe?

Because that mapping is lost, whoami can't convert your ID into a username so returns the ID, and your prompt defaults to the "I have no name!" message because you effectively don't have a name.

Logging out and back in might fixit, but restarting is the best way (as you discovered).

Basically, it's a symptom of another problem, and not a problem in and of itself.


I see it's an old thread, but here is the solution to this problem (without restarting the whole computer).

The problem is in the opendirectoryd daemon and the earliest reports date from the beginning of 2011. Restarting the daemon (switching users with an admin one via Fast User Switching) fixes the problem.

While writing this answer, I've found a similar question in the Serverfault here, which also covers my answer.