It is a mistake to believe that the ego is strictly an enemy in the spiritual quest, and more so to believe that it is the only obstacle. As with so many things, the nuance has been washed out of this teaching as dye from cloth drying in the sunshine.
Part of the problem is that “ego” is often misidentified with “mind”. In Sāmkhya, Yoga, and Vedānta, however, the student-practitioner comes to learn from experience that these two are distinct entities. It is true that in daily life they are identified with one another (and generally with the body, as well), but much of the process of Yoga is, at first, analytical. It is necessary to discern where and when mistakes are made before they can be corrected and prevented, and this process of discernment must begin by determining where one “substance” ends and the next begins.
Patānjali opens his famous Yoga Sūtras thus: “Yoga is stilling the modifications of chitta. Then the seer abides in itself. Otherwise, the Self is identified with the modes of chitta.” (sūtras 1:2 – 4) It is not necessary, just now, to discuss the five modes of chitta, but it is important to know the sheaths through which they operate.
Chitta is none other than the stuff of consciousness; it is not consciousness itself (which is the Ātman of Vedānta and the puruṣa of Sāmkhya), but rather its reflection in the substance of Nature (prakrti). Chitta is sattvic — light, pure, and peaceful — by nature, but when perturbed by the activity of the other qualities/gunas (passion/rajas, and inertia/tamas), chitta refracts pure consciousness into three components, much as white light refracts into three primary colors. These components are not pure, so they are all a mix of the activity of the three gunas: buddhi (intellect) is the most sattvic; ahamkāra (ego) is the most tamasic; manas (mind) is the most rajasic.
Most of us, most of the time, are so mentally muddled that we simply cannot tell the difference between these three functions. We conflate emotions and sensations with our very sense of self — “I am sad,” or “I am happy” — and confuse preference or taste for discernment. So, the yogin first learns to watch himself closely, to see the movements of the mind in both general patterns and details. From here is analysis possible, whereby the sense of “I” may be separated out from thoughts and emotions.
The process then turns to one of discernment; the intellect is uncovered as the ego detaches from identification with the mind. The thoughts and sensations of the mind are then accepted or rejected according to their correspondence to reality, or else their relative usefulness. Once well-established, even this process shifts; the intellect’s job, essentially, is to bring the ego into identification with chitta itself. The entire process of analysis, discernment, and discrimination is a gradual awakening to the function of consciousness within Nature, enabling the ego to draw back to its source in chitta.
To fight against “ego” is therefore to fight against the reflection of self-awareness within individual consciousness. It is a battle which cannot be won. Ego itself is not the problem; the problem is wrong identification of “I” with anything else. Sri Ramana Maharshi taught to trace the sense of “I” to its root; the Buddha taught to discern the emptiness of the constructs which we call “I”; Patānjali taught to practice detachment from the “not-I” in order to still the modifications of chitta. These are all the same teaching, with the same goal.
None of this is to say that ego is inherently good or helpful, either. Ego is the Original Sin, the First Error, the sense of self apart from Self. Still, ego is effectively a neutral entity of mixed nature, an ersatz arising, so to speak, between Spirit and matter. As nothing more than an abstract sense of “I am,” ego’s moral and spiritual value is entirely dependent upon its point of identification. When ego says, “I am this body,” or “I am this mind,” or anything other than simply “I am,” we call this delusion. When, through the intellect’s power of discrimination, “I am” rests as “I am,” chitta is still and the illusion of individuality resolves itself.