Afridi returned figures of 5 for 32, while his compatriots Hasan Ali (2-52) and Sajid Khan (3-33) helped bundle out the Tigers for just 157 on Monday.
The hosts lost their last four wickets for just four runs at the Zahur Ahmed Chowdhury.
Chasing 202 to win the game, the visitors’ openers breezed to 109 at stumps, leaving Pakistan just 93 runs shy of the target with a day in hand.
Shafique became the fifth Pakistani batsman to score half-centuries in either innings on debut while the opening pair is only the second from the country to put up century stands in both innings of a Test match.
Rather than forcing the issue, the duo waited on the deliveries to guide them square of the wicket.
Bangladesh’s hopes stuttered early in the morning when Mushfiqur Rahim (16) departed off the third ball of the day.
Nightwatchman Yasir Ali (36) added 47 runs in a sixth-wicket stand with Liton Das (59), but neither looked comfortable against the sting of the Pakistan bowlers.
Centurion in the first innings, Liton was given a reprieve when Mohammad Rizwan missed a stumping opportunity while Yasir inside-edged a couple of boundaries after nearly running himself out.
Bangladesh’s chances of a revival ended when Afridi’s bouncer crashed into the back of Yasir’s helmet. The batsman retired hurt seven deliveries later and needed medical scans.
Mehidy Hasan Miraz (11) was sent packing leg-before before Yasir’s replacement Nurul Hasan (15) holed out at long-on, both dismissed by Sajid.
Liton followed them soon after, trapped in front by Afridi, whose ripper saw the back of Abu Jayed for his fifth, taking his 2021 tally of Test wickets to 44 in the process. Sajid picked up his third with the scalp of Taijul for a duck.