U.S. Army Sergeant Bowe Bergdahl on Friday was spared prison time for endangering fellow troops when he deserted his post in Afghanistan in 2009, but a military judge ordered he should be dishonorably discharged from the service.

Bergdahl, now 31, was captured by the Taliban and spent nearly five years under brutal captivity by the insurgent group. He had faced up to life imprisonment after pleading guilty to desertion and misbehavior before the enemy in a case that sparked debate on whether Bergdahl was a villain or a victim.

The sentence drew swift condemnation from President Donald Trump, who called it "a complete and total disgrace to our Country and to our Military" in a Twitter post from Air Force One as he flew on the first leg of a trip to Asia.

As a Republican candidate for president last year, Trump, now the military's commander in chief, called Bergdahl "a no-good traitor who should have been executed."

The decision on Sergeant Bergdahl is a complete and total disgrace to our Country and to our Military. —@realDonaldTrump

In a military courtroom at Fort Bragg in North Carolina late Friday morning, Bergdahl trembled as he waited to hear his punishment.

Army Col. Jeffery Nance delivered the sentence in a hearing that lasted just two minutes, and did not comment on his decision. He also recommended that the Hailey, Idaho, native be demoted to private and forfeit $10,000 US in pay.

Won't be eligible for benefits

Defence lawyers, who had urged Nance to show leniency, said Bergdahl was relieved and eager to move on. "This has been a terrible ordeal," Eugene Fidell, one of the soldier's lawyers, said after the hearing.

Prosecutors, who had sought 14 years of confinement, did not comment.

A dishonorable discharge, given for the most serious offenses, typically means a loss of all veteran and military benefits, including health care benefits, from the Department of Veterans Affairs.

President Trump's unprincipled effort to stoke a lynch-mob atmosphere while seeking our nation's highest office has cast a dark cloud over the case. - Eugene Lidell, lawyer for Bergdahl

That could pose difficulties for Bergdahl, who, according to testimony, suffers significant nerve damage as a result of malnutrition and torture while a prisoner of the Taliban and has several mental health conditions.

The discharge will not take effect until an appellate court affirms his conviction and sentence.

Bergdahl's actions in Afghanistan drew withering criticism from political leaders in Washington and fellow soldiers, both for the dangerous efforts to find him and the Taliban prisoner swap brokered by the Democratic Obama administration that secured his release.

In May 2014, Bergdahl was handed over to U.S. special operations forces in Afghanistan in exchange for five Taliban detainees, who were released from Guantanamo Bay prison and flown to Qatar.

Nance, acting on a defence motion, had previously ruled that Trump's comments had not influenced him nor affected Bergdahl's chances of a fair sentence, but said he would consider them a mitigating factor. Defence attorney Fidell said on Friday that Americans should be offended by Trump's behaviour.

"President Trump's unprincipled effort to stoke a lynch-mob atmosphere while seeking our nation's highest office has cast a dark cloud over the case," the lawyer said.

Defence lawyers had argued during Bergdahl's sentencing hearing that he was a young, hardworking soldier who did not understand the full consequences of his actions when he deserted.

Bergdahl, who has said he wanted to report problems in his unit, apologized in court this week for the suffering he caused his comrades and admitted he had made "a horrible mistake."

Fidell said Bergdahl was especially grateful for "those who heroically sustained injuries" searching for him after he left his combat outpost in Paktika province in June 2009 without permission.

Prosecutors, however, said Bergdahl knew his disappearance would trigger alarm in the war zone.

They acknowledged that Bergdahl suffered during his years as a prisoner of the Taliban, but argued that did not diminish the pain of fellow service members who were wounded during the futile hunt for him.