1530s, "distinguished, eminent, excellent," from Latin egregius "distinguished, excellent, extraordinary," from the phrase ex grege "rising above the flock," from ex "out of" (see ex-) + grege, ablative of grex "a herd, flock" (from PIE root *ger- "to gather").
Disapproving sense, now predominant, arose late 16c., originally ironic. It is not in the Latin word, which etymologically means simply "exceptional." Related: Egregiously; egregiousness.