A former military commander with Al Shabab, the Somali terrorist group with ties to Al Qaeda, has become a cooperating witness and is expected to testify for the United States government in a trial this summer in Manhattan, prosecutors have disclosed in a court filing.

More than 30 defendants have been prosecuted in this country for supporting Al Shabab; the former military commander would appear to be the most senior cooperating witness from that group to testify in an American trial, several terrorism experts said.

The former commander, who is referred to in the filing only as CW-2, is assisting the government in the prosecution of Mohamed Ibrahim Ahmed, who is accused of being a Shabab conspirator. His case in Federal District Court in Manhattan has been full of intrigue and has caused a legal battle over government interrogation tactics.

The former Shabab commander would appear to be able to offer an insider’s view of the group and of Mr. Ahmed, an Eritrean scheduled for trial in July, the government’s filing suggests.

Insider testimony in terrorism cases can be crucial, as was highlighted in the recent trial of a Queens man convicted in a plot to stage suicide attacks in the city’s subways. Four admitted terrorists testified at the trial; and the jury heard testimony about Al Qaeda and its training camps in Afghanistan and Pakistan.

“When you have people who have been on the battlefield, trained, who espouse the ideology,” Karen J. Greenberg, director of the Center on National Security at Fordham University School of Law, said, “and who are truly inside the system and not viewing it as an expert or an academic, the impact on the jury and the long-term legitimization of the case is greater.”

The former commander, prosecutors wrote, will testify that he encountered Mr. Ahmed in Somali in 2009 and that Mr. Ahmed traveled with him and his group of fighters in a convoy near the Somali coast.

During a stop, Mr. Ahmed told the commander that he had European citizenship (he had lived in Sweden) and had come to Somalia to fight, which the commander understood to mean engage in jihad, prosecutors said.

Mr. Ahmed made his way to Nigeria, where he was taken into custody and interrogated by the Nigerian authorities, who suspected him of being a Qaeda agent, a Nigerian investigative report shows.

While in Nigerian custody, he was also questioned by United States officials for intelligence purposes, without being advised of his Miranda rights. Then, in sessions that began about a week later, he was read his rights, waived them, and was interrogated by F.B.I. agents, the government has said.

Prosecutors have said they plan to introduce Mr. Ahmed’s statements to the Nigerians, as well as his statements to the F.B.I. agents. Mr. Ahmed’s lawyers have asked a judge to suppress the statements, on the grounds that they were the product of coercion and that the two-step interrogation strategy prevented him from making a knowing and voluntary waiver of his right against self-incrimination.

The judge, P. Kevin Castel, has not yet ruled on the defense motion.

Mr. Ahmed has been charged with providing material support to a foreign terrorist organization; an indictment says he trained with Al Shabab in Somalia and contributed money to the group.

An F.B.I. account of statements by Mr. Ahmed shows that when he was living in Sweden, Swedish and British authorities tried to recruit him as an informant, approaches he appears to have rejected.

“These people are trying to use him and turn him against what he believes in,” the F.B.I. account says. “Ahmed stated that it’s better to die as a man than a dog, and explained that this meant that it’s better not to be forced into doing anything you do not believe in.”

In the recent court filing, prosecutors said that they also planned to call another cooperating witness against Mr. Ahmed, who is identified as CW-1. The witness is expected to testify that he was present with Mr. Ahmed in the 1990s at a Qaeda training camp in Afghanistan, prosecutors wrote.

Though the government’s filing does not identify CW-2, the former Shabab commander, the description of him resembles that of Ahmed Abdulkadir Warsame, a Somali captured last year by the United States military in the Gulf region and questioned for intelligence purposes aboard a naval vessel for about two months. Later, he was brought to Manhattan and charged with providing material support to Al Shabab, and to Al Qaeda in the Arabian Peninsula, in Yemen.

In July 2011, when the case was revealed, an American counterterrorism official described Mr. Warsame as a “senior operational commander” of Al Shabab; and a government news release called him a “Shabab leader.” But another official played down his role, suggesting that he was important only because of the intelligence he had provided during his interrogations.

A federal indictment says Mr. Warsame worked as a liaison with the Qaeda group in Yemen, providing it with money, training and equipment.

The Justice Department, the United States attorney’s office, the F.B.I. and Mr. Warsame’s lawyer all refused to comment about his case or about the cooperating witness.

Sabrina Shroff, a lawyer for Mr. Ahmed, declined to comment, other than to say that the “classified nature” of such cases would not allow for a true picture of any cooperating witness.