Thirty miles from the centre of Bangladesh’s chaotic capital, Dhaka, the pace of life is quieter. At the Baroipara checkpoint, near the industrial area of Ashulia, stalls offer tea and snacks to local workers and visitors to a nearby public park.

At 7.30am on 4 November, five policemen were dropped off at the checkpoint for a day-long shift and sat down for tea on benches shaded by tall, slender sal trees. The attack was so sudden they could barely react. Their assailants rode up on motorbikes, dismounted and then struck out with machetes. Two of the policemen were badly injured. One bled to death on the dusty roadside.

Tea is no longer being served at Baroipara. “We have stayed shut out of fear,” said Shamsul Alam, the owner of one stall. “I’m scared. If a cop can be killed, who knows what can happen to common people like us?”

Alam is not alone. The Baroipara killing came after the murders of two foreigners – an Italian and a Japanese national in Bangladesh – which were both claimed by Islamic State, which is suspected of bombing the Russian holiday jet in Egypt last weekend. Then there has been the series of murders of secular intellectuals, bloggers and publishers. Five have now died over the last year. These murders have been claimed by a local affiliate of al-Qaida in the Indian subcontinent.

Many in Bangladesh, a Muslim-majority country of 160 million-plus, are now frightened. “I am working in the university and all my friends are really panicked. No one knows what could happen or when,” said Shantanu Majumder, a political scientist and activist. “Bangladesh has changed completely. This is not our usual, easy life. The [extremists] may not be strong enough to start a civil war, but they are strong enough to continue their killing mission.”

There is no doubt that such anxiety is real and justified, and al-Qaida and Isis have thus succeeded in attaining the goal of all such militant groups: to terrorise their enemies, mobilise existing supporters and polarise communities to create the conditions that favour extremism.

The entry of the two groups into Bangladesh is relatively recent; the ground for their horrific campaigns of violence there has been prepared by historical, social, economic and political factors reaching back almost 50 years, if not longer.

The borders of modern Bangladesh were drawn in 1947, when the British divided their south Asian empire. Most Muslim-majority areas became part of Pakistan, which was itself split into east and west wings. In 1971, civil war broke out between local nationalists in East Pakistan who wanted independence and those who did not. The former, on the whole, spoke the local language of Bangla, favoured socialist-style economic policies and desired a liberal, secular framework for their new nation. Most of those who favoured a continued union with Pakistan were religiously and socially conservative.

The ensuing conflict was brutal, with hundreds of thousands killed and some of the worst atrocities committed by militias recruited by Pakistani forces, but resulted in an independent country with a secular constitution. The scars have never healed, hardening into permanent political, social and cultural divisions which have marked every aspect of Bangladesh’s recent history.

Bangladesh’s prime minister Sheikh Hasina. Photograph: Oli Scarff/Reuters

The current government is headed by Sheikh Hasina, the daughter of Sheikh Mujibur Rahman, the most prominent independence leader, who was assassinated in 1975. She heads the Awami League. The rival Bangladesh Nationalist party (BNP) has long represented more conservative elements and is led by Khaleda Zia, the widow of the military dictator who took power in the aftermath of Mujibur’s death and was killed in 1981. The two women make little effort to hide their mutual hatred.

Then there is the Jamaat-e-Islami, an Islamist political party and social movement which resisted independence and was implicated in some of the worst abuses.

All the parties have battled bitterly over the years, through the ballot box, political manoeuvres, cynical short-term alliances and on the streets. But if history has left a troubled legacy, so too has economic change.

The view from the battered bridge over the Buriganga river in the centre of Dhaka is apocalyptic. In the foreground are the black, heavily polluted waters of the river itself. Beyond lies Kamrangir Char, a vast slum where clouds of acrid smoke from burning rubbish hide tenements packed with thin men, anxious women and grubby children with tubercular coughs.

But Kamrangir Char is also a thriving community and informal economic hub. Millions flock to similar slums in Dhaka and elsewhere every year, flowing in from rural areas hit by climate change-induced flooding, overcultivation and unemployment. For all its problems, the slums, and Bangladesh’s chaotic, congested cities more generally, act as launching pads into better lives than many could ever have lived in their remote villages.

Recent years have seen steady economic growth. Incomes and literacy levels have risen. Infant malnutrition and maternal mortality are down. One factor is the $25bn garment business, which makes cheap clothes for the west and employs at least four million people. The deaths of 1,000 workers when a garment factory collapsed in 2013 underlined problems of poor safety and conditions, but the days when Bangladesh was dismissed as a “basket case” are long gone.

Women at work in a Bangladeshi garment factory. Photograph: Demotix/Corbis

As elsewhere in the Islamic world, however, development has not ended the threat of radicalism. Indeed, some believe it may even have fuelled it.

According to one western intelligence official based in the region, those least equipped to thrive in the new better-off Bangladesh are young men, often from rural backgrounds but raised in the big city, whose poor education and lack of skills, social or professional, is a huge disadvantage amid cut-throat competition for jobs, money, status and eligible marriage partners. “Many, many Islamic militant recruits in Bangladesh and more broadly across the Islamic world match that profile, and that is no coincidence,” the official said.

Others, however, deny the link. “In principle, everyone wins from growth. This is a broader phenomenon. No Islamic country has been untouched by the global crosscurrent of radicalism in recent years,” said Ahsan Mansur, executive director of the Policy Research Institute, a Dhaka-based thinktank.

As elsewhere, Bangladesh’s new global commercial and cultural links are also a factor. The millions of Bangladeshi migrant workers in places such as Qatar or Saudi Arabia are exposed to austere styles of Islam very different from the traditional, folksy forms of worship of their homeland. Over decades, cash from the Gulf has funded religious seminaries and institutions in Bangladesh.

Clerics at one such school in Kamrangir Char told the Guardian two years ago that their primary aim was to stop “alien [western] culture” making inroads in Bangladesh. Several of those detained for the recent militant attacks were seminary students. Such institutions teach hardline unitarian dogmas which, even if they are theoretically non-violent, are certainly intolerant.

Then there is the impact of nearly 15 years of conflict, invasion and rhetoric across the Islamic world, relayed by social media. This, according to columnist Shuprova Tasneem, has contributed to “a growing distrust of western concepts of secularism, viewed as an ideology that not only disassociates religion from state affairs but attempts to reduce its influence in our private lives as well.”

There have long been signs of rising radicalism. Attacks on Christians and Hindus have intensified. Last month Bangladesh’s small Shia Muslim community – reviled as heretics in many Gulf countries – were targeted by a bomb claimed by Isis. Yet the final impetus to the new violence may have come from the very people who say they want to stop it.

Shortly after returning to power in 2009, the new Awami League government set up a special tribunal to try those allegedly responsible for atrocities in the 1971 war. The party said it was bringing closure through justice. Many, though not all, of those in the dock were senior Islamists, and human rights groups were critical of the trials. The sentences that were pronounced by the court led to huge protests and counter-protests by supporters and opponents of the process. “The trial of the war criminals is key. The problems all started then,” said Majumder, the political scientist.

Jamaat-e-Islami supporters set fire to a bus in protest at sentences handed down by a war crimes tribunal in 2013. Photograph: Reuters

In January 2014, Bangladesh held a widely criticised election boycotted by the BNP and other opposition parties. The Awami League won, but the poll triggered intense street violence. Hundreds were killed or injured in firebombings and riots as the opposition tried to oust the government. With many of its leaders detained or in hiding, the BNP has been almost eliminated as a political force. So, too, have the Islamists of Jamaat-e-Islami. The result is no credible opposition – and no democratic channel to express dissent.

“The crackdown has been so hard that it could become a self-fulfilling prophecy in terms of extremism,” said Michael Kugelman, a south Asia expert at the Woodrow Wilson Centre in Washington DC.

Sheikh Hasina, the prime minister, denies that Isis exists in Bangladesh. She blames opposition parties both for the recent killings and for the broader problem of extremism. This, said Kugelman, indicated a “complacency” that was “counterproductive at the very least”.

Few believe either Isis or al-Qaida has a substantial presence in Bangladesh. But, despite attracting only a tiny fraction of Bangladeshis, it is clear that both have enough followers to continue, and possibly expand, the current campaign of violence.

Mukul Hossain, the policeman killed in Wednesday’s attack, was buried at his village home in Bogra, 150 miles north-west of Dhaka. Speaking on condition of anonymity, a colleague expressed, with some understatement, the sentiments of many: “Our country has been peaceful, but recently it has become a bit troubled,” he said.

Additional reporting by Saad Hammadi in Ashulia

NATIONAL PROFILE

Population 159.1 million, with an annual growth rate of 1.2%, rising to 3.6% in urban areas.

Capital city Dhaka (population: 16.9m).

Area 147,570 square kilometres.

Life expectancy 71.3 years for women, 69.8 years for men.

Unemployment 4.3%. But more than 25.6% live below the poverty line.

Youth literacy rate 78% for men, 82% for women aged 15–24.

Education 20% of the total population are students (around 29 million).

Gross domestic product $173.8bn.

Source: UN data/World Bank