Shave and a haircut

I'm having trouble finding the meaning and origins of the phrase: shave and a haircut

I've heard it can also be taken a derogatory phrase, but don't know how

It was a fairly standard order in old fashioned men's Barbers Shops and meant what it said. I've never heard of it used in any other context. Have others?

The only time I've heard of it is as a little "riff" at the end of a song. "Shave and a haircut, two bits" or "five cents."

I'm familiar with "shave and a haircut" referring to a series of sounds such as raps on a door, something like [-..--], with two little short ones for "and a" (unstressed words here). The addendum "two bits" (the shave-plus-haircut's price, 25 cents) is just two sounds, [--], either added by the original 'speaker' or given as a response.

It is sometimes said that the same little 'tune' is used for an obscenity in Spanish.