SOUSSE, Tunisia — Dressed in black shorts and a T-shirt, the young man walked purposefully along the beach but looked much like a tourist — until he unwrapped an assault rifle and opened fire, first into the sand and then in an arc at sunbathers.

“He seemed like he did not know how to handle the weapon, because it is heavy,” said one witness, Hassen, 30, who declined to give his full name. “He seemed not to be experienced.”

Yet the gunman was silent and businesslike, Hassen and other witnesses said on Saturday. As the tourists fled and saw friends falling, he pursued the sunbathers from the beach to the pool and eventually to the administration offices on the second floor of the Imperial Marhaba Hotel here, killing 39 people and wounding 38 others in all on Friday.

Later identified by the authorities as a 23-year-old Tunisian student, Seifeddine Rezgui, the gunman was eventually shot and killed by a policeman, but not before carrying out Tunisia’s worst terrorist attack in living memory.