The abrupt announcement came in a tweet from the president, who said Shanahan had done "a wonderful job" but would step aside to "devote more time to his family."
The president added that the Secretary of the Army, Mark Esper, will be the new acting secretary.
"I know Mark, and have no doubt he will do a fantastic job!" Trump said in a second tweet on the subject.
The post atop the Pentagon has not been filled permanently since Gen. James Mattis retired in January following policy differences with Trump.
Trump announced in May that he would nominate Shanahan but the formal nomination process in the Senate had been inexplicably delayed
Shanahan, a former Boeing executive, has been leading the Pentagon as acting secretary since Jan. 1, a highly unusual arrangement for arguably the most sensitive Cabinet position.
Shanahan, 56, had extensive of experience in the defense industry but little in government. In more than four months as the acting secretary, he focused on implementing the national defense strategy that was developed during Mattis' tenure and emphasizes a shift from the resources and tactics required to fight small wars against extremist groups to what Shanahan calls "great power" competition with China and Russia.