FORT BRAGG, N.C. — Army Sgt. Bowe Bergdahl, who walked away from his post in Afghanistan and triggered a search that left some of his comrades severely wounded, was spared a prison sentence by a military judge Friday in what President Trump blasted as a “complete and total disgrace.”

The judge gave no explanation of how he arrived at his decision, but he reviewed evidence that included the five years Bergdahl was held captive by the Taliban and the wounds suffered by troops who searched for him, including one who now uses a wheelchair and cannot speak.

The case was politically divisive. President Barack Obama traded Taliban prisoners to bring Bergdahl back, drawing sharp Republican criticism. As a presidential candidate, Trump called for the soldier to face stiff punishment. He could have received up to life in prison.

The judge also gave the 31-year-old a dishonorable discharge, reduced his rank from sergeant to private and ordered him to forfeit pay equal to $1,000 per month for 10 months.

In court, Bergdahl appeared tense, grimaced and clenched his jaw. His attorneys put their arms around him and one patted him on the back..

Bergdahl “is grateful to everyone who searched for him,” especially those who “heroically sustained injuries,” defense lawyer Eugene Fidell said.

Trump’s statement came in a tweet about 90 minutes after the sentencing. “The decision on Sergeant Bergdahl is a complete and total disgrace to our Country and to our Military,” the president wrote.

Bergdahl pleaded guilty last month to desertion and misbehavior before the enemy. He has said he left his post in 2009 with the intention of reaching other commanders and drawing attention to what he saw as problems with his unit.

The judge, Army Col. Jeffery Nance, had wide leeway in deciding the sentence because Bergdahl made no deal with prosecutors to limit his punishment.

Prosecutors sought a serious penalty because of wounds suffered by service members who searched for Bergdahl after he disappeared.

The defense tried to counter that evidence with testimony about Bergdahl’s suffering as a captive, his contributions to military intelligence and survival training, and his mental health problems.