Use:

sw_vers

In my case:

ProductName:    Mac OS X
ProductVersion: 10.6.4
BuildVersion:   10F569

Update: This still works in os x yosemite:

ProductName:	Mac OS X
ProductVersion:	10.10.2
BuildVersion:	14C1514