I’m currently digging into the source of virtualenv, a python utility for managing environments. It’s incredibly handy, and I’d been using it for a while without really understanding how it worked. I’ll have more later on the details, but in the meantime, I hit a particularly puzzling bit of code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
This is largely intelligible - clearly we’re modifying the $PS1 variable, which sets the bash prompt. But what on earth is this line?
x is not a variable defined elsewhere in the script, and bash will interpret both
x as string literals anyway. Is there some bizarre shell in which this test can possibly return true?
As it turns out, there isn’t. The reason this code looks so bizarre is that it’s automatically generated by virtualenv. The original line of code is this:
And then elsewhere we find its complement:
Together, this code is entirely reasonable.
Thanks to user ruakh on Stack Overflow for the key insight here.