Rebel militiamen, many of them former prisoners of Muammar Gaddafi, have an iron grip on Tripoli, but wonder who will hold long-term power in Libya. Issam, a bearded Islamist who controls a district of the revolutionary stronghold of Souq al-Jumaa, holds permits which allow him to field 70 local militiamen with arms. “We do not have that number of weapons,” he says. For the moment he does not need them because Gaddafi loyalists “have turned 180 degrees and joined us or they are staying home after we took away their guns”.

A year ago Issam was in jail because the security forces imprisoned anybody they suspected of Islamist tendencies at the time of the anniversary of Gaddafi’s 1969 coup, which was presented as a popular revolution. “They would put me in prison for a month because of my beard which they thought Islamic,” recalls Issam. “I was lucky because many of my friends got two or three months just for being bearded.”

Much of government propaganda on Libyan state TV during the six-month civil war was focused on claims that the rebellion was being led by Islamic militants linked to al-Qa’ida. The aim was to frighten the US and Nato abroad and secular Libyans at home.

But the allegations also had some credibility, since the most militant and experienced anti-Gaddafi organisation was the Libyan Islamic Fighting Group (LIFG). It is Abdul Hakim Belhaj, one of the founders of this group, which fought a ferocious guerrilla campaign against the regime in the 1990s, who now commands the militia units in Tripoli.

Mr Belhaj, 45, a veteran of the Afghan war in the 1980s, is a leading jihadi who was arrested at American behest in Malaysia in 2003, tortured in Thailand and handed over to Libya in 2004. At that time US and Libyan intelligence were co-operating in eliminating Islamists and American officers allegedly took part in interrogations in Tripoli.

Released along with other militants in 2010 by an over-confident regime, Mr Belhaj was well-placed to play a leading role in planning and carrying out the assault that captured Tripoli.

The anti-Western attitudes of Libyan Islamists have been changed by the knowledge that without Nato air strikes Gaddafi would have won the war. The long term aims of the LIFG may not have changed much, but, as with Islamists in most countries convulsed by the Arab Spring, they long ago recognized that they needed to be allied to liberals and secularists to overthrow police states in Syria, Egypt, Tunisia and Libya. They also needed to distance themselves from al-Qa’ida to avoid being denigrated as international pariahs, as Gaddafi tried to do.

The Islamists may, for the moment, look stronger than they are because they alone could provide experienced guerrilla leaders and an organized network of sympathizers in Tripoli. These cells could scarcely maintain their existence before the rebellion started on February 15, but thereafter they expanded rapidly.

Abdel Razzaq Targhuni, a Libyan currency exchange dealer with Islamist sympathies living in Dubai, recalls how he came home in late February and started to organize against Gaddafi in the wealthy Andalus district of Tripoli. “Before that there wasn’t much contact between cells,” he says.

The loyalties and divisions of the new Libyan armed forces remain mysterious but there are signs of trouble ahead. Lt Col Khalid Bilad, an officer in Libyan Special Forces, who defected at the start of the revolution, was captured, imprisoned and tortured before escaping during a Nato raid and then helped plan an insurrection in Tripoli.

His unit, once called the “Tripoli Lions”, is now joining the main militia body and – for reasons the colonel refused to explain – is naming itself after the murdered commander-in-chief of the rebel army Abdul Fatah Younes, Gaddafi’s former Interior Minister, who was probably killed by Islamists.