With Wednesday's removal of Egyptian president Mohamed Morsi, the Egyptian military asserted itself as the predominant force in the nation for the second time in three years.

It was the military that ousted longtime president Hosni Mubark in 2011, in what was then the most dramatic and consequential development of the Arab Spring.

This week, Egyptian supreme commander Lt. Gen. Abdul-Fattah el-Sisi deposed Morsi, head of the Muslim Brotherhood, after the military's supreme council had issued the president an ultimatum: meet street protesters' demands for a more inclusive government that would strengthen the voice of opposition parties or be forced from office.

Since Mubarak's ouster, the military has portrayed itself as a guarantor of national integrity and as a neutral defender of the people's hard-won freedom. This week, hordes of protesters are hailing the military as guardians of the revolution.

But the military's rise to its current outsized role has been far from straightforward, its historic relationship with Egypt's civil society a complex tango of patriotism and self-interest, victory and defeat.

Independence, Revolution, and Defeat

Though Egypt gained its independence from Britain in 1922, the British continued to occupy the country with its soldiers for the next 25 years.

"The Egyptian army was a relatively weak institution during the period between the two world wars," says Joel Beinin,professor of Middle East History at Stanford University. "[The British] were calling the shots, and they did not want to deal with a potentially strong army."

While a small number of Egyptian soldiers fought alongside British forces during World War II, the Egyptian army had little standing with public and a very limited role in government affairs.

All that changed in 1948.

That was the year the state of Israel was born, inciting the first Arab-Israeli war. Over a year of intermittent combat, a coalition of Arab armies was beaten back by Israeli defenders.

The defeat undermined Egyptian monarch King Farouk, whom the Egyptian military blamed for what it considered a fiasco, and in 1952 a group of military officers led by Gamal Abdel Nasser staged a coup. The Egyptian Revolution of 1952, as it came to be known, established the modern Egyptian republic.

Under the charismatic and popular Nasser, the Egyptian Army sought to rebuild itself into a competent fighting force. It didn't go well.

After a costly expeditionary campaign supporting Arab republican forces in Yemen's civil war, Nasser led a second Arab coalition against Israel during the Six Day War of 1967. His forces suffered a spectacular defeat, losing the the entire Sinai peninsula—which Egypt had controlled—to Israeli forces.

The loss shredded the Egyptian army's credibility. The military "not only withdrew [from Sinai], but in fact it lost its equipment and many soldiers came back running," says Al-Sayyed. "The military became the butt of jokes within the Egyptian public."

Redemption and Expansion

Following Nasser's death in 1970, Anwar El Sadat acceded to presidency. Initially perceived as a caretaker president, Sadat proved people wrong by purging the government of Nasserists and reforming an Egyptian military that was still nursing its wounds from the Six Day War.

In October 1973, Sadat led the third Arab coalition against Israel in the hopes of regaining the Sinai.

Although the war ended after three weeks with Israel in the advantage, the Egyptian Army enjoyed significant successes during its opening days, and Egypt later regained the Sinai peninsula as a result of the 1978 Camp David Accords.

The win destroyed the myth of Israeli invincibility and restored the reputation of the Egyptian army as a fighting force and defender of the nation's interests.

After Mubarak, a former general, took power following Sadat's assassination in 1981, the military, backed by American aid, modernized and expanded its force. And when Mubarak launched economic liberalization in the 1990s, the military discovered something even better than American largesse: capitalism.

"The military became an economic company, if you will," says Al-Sayyed. "It became an enterprise."

Equipped with valuable and vast real estate and a conscript, low-paid workforce, the military began to insinuate itself into civil society through business, its holdings ranging from bread factories to chemical plants to hotels.

The armed forces' public-private enterprises may account for up to 15 to 20 percent of GDP, according to Al-Sayyed, and the military took very good care of its officers with the wealth it accrued. As the military's economic tentacles spread throughout society, its civil clout expanded, too.

And yet despite its burgeoning business portfolio, the military's senior officers actively avoided the political limelight.

Since 1973 the military "has kept largely out of political sight, tacitly guaranteeing the rule of successive, unpopular rulers, while not being visibly connected to them," says Steven Simon, Executive Director of the International Institute for Strategic Studies in Washington, DC.

That dichotomy—aggressive economic expansion without the visible political clout that could have come with it—allowed the military to cloak itself in a neutral patriotism while advancing its own self-interests, says Joel Beinin of Stanford University.

What Way Forward?

During the massive protests that led to Mubarak's ouster, the military—after a period of initial silence—publicly stated that its duty was above all to the people of Egypt, and soon wrested control from the 80-year-old autocrat.

During the period of military rule that followed, however, public opinion turned against the interim military government due to the widespread belief that it was dithering in relinquishing power.

The military seemed to recognize the fickle nature of voters in the drafting of Egypt's 2012 constitution, ensuring that its privileges and powers were cemented in the new document.

Now, after unseating the unpopular Morsi, the military has public opinion back in its court.

"The majority of Egyptian people today see the army as a patriotic institution that can be trusted to act in the interests of the nation," says Beinin. "The army has this reputation despite the fact that it has actually on many occasions, and especially in the recent years, acted to secure its own particular institutional interests and not acted for the interests of the nation."

On Thursday, General el-Sisi stated once again that the military existed only to serve the will of the people. Whether Egypt's military leaders were acting out of patriotism or self-interest or some combination of both, only history will tell.