Shakib was unbeaten on 136 at the break and skipper was Rahim 28 not out as Bangladesh added 51 runs to their overnight total of 234-5 after a delayed start due to fog.
Pakistan took the second new ball immediately after it was due but pacemen Umar Gul and Aizaz Cheema failed to break the Shakib-Rahim stand.
Former captain Shakib, 108 overnight, has so far hit 15 fours in his 229-ball knock and added 62 runs for the sixth wicket with Rahim.
Rahim, five not out overnight, played some handsome shots in the morning as he slog-swept off-spinner Saeed Ajmal over mid-wicket for the first six of the match and then cut left-arm spinner Abdur Rehman for a four in the next over.
Bangladesh were in deep trouble at 43-4 on Saturday after being put into bat but Shakib and Shahriar Nafees (97) rescued the home side with a stand of 180 runs for the fifth wicket.
Pakistan lead 1-0 in the series after winning the opening Test in Chittagong by an innings and 184 runs.