Somerset 209 for 8 (Imam-ul-Haq 90, T. Price 4-49) trail Gloucestershire 343 (Harris 159, Charlesworth 56, O. Price 52, Gregory 4-62) by 134 runs
After dismissing their opponents for 343, from an overnight 320 for 6, the home side plunged to 87 for 4 and looked in danger of failing to reach the follow-on mark of 194 against their arch-rivals.
But Pakistan Test opener Imam, signed for the last four Championship games as Somerset seek to avoid relegation from Division One, helped spare his team that embarrassment by facing 162 balls and hitting nine fours.
Thanks largely to the 26-year-old left-hander’s model technique, the home side reached 209 for 8 by the close of a rain-interrupted day, but still trailed by 134.
The start of play at the Cooper Associates County Ground was delayed until 11.15am after heavy overnight rain. Momentum was with Somerset from the final session on day one and their bowlers wasted no time building on it.
With only eight runs added, Tom Price was caught behind driving at a wide delivery from Lewis Gregory, who had David Payne caught at mid-on next ball.
Jack Taylor had added just five to his overnight 15 when mistiming a drive off Josh Davey and offering a return catch before Gregory ended the innings by pinning Ajeet Dale leg before.
Gregory finished with 4 for 62 and Davey 3 for 80. Gloucestershire’s last nine wickets had fallen for 92 runs, but soon it was their opponents’ fragile batting line-up being exposed.
Tom Lammonby was dropped at second slip by Marcus Harris in the opening over, but then nicked the next ball, another Payne outswinger, to wicketkeeper James Bracey.
It was 17 for 2 when skipper Tom Abell edged a back-foot shot to Ollie Price at second slip off Dale. George Bartlett helped Imam steady the ship, contributing 33 to a stand of 57 either side of lunch before losing his off stump, shouldering arms to Price.
With a further 13 added, Lewis Goldsworthy glanced a leg-side delivery from Price through to Bracey and Somerset were in a deep hole.
But by then Imam was well set. Like Gloucestershire centurion Harris the previous day, he demonstrated his Test quality by mixing solid defence with an unfailing ability to punish anything loose.
Imam moved to a flawless fifty off 108 balls and found an equally solid partner in fellow left-hander Rew as the pair took the score to 135 for 4 by tea.
The 18-year-old wicketkeeper has displayed an excellent temperament in his early Somerset appearances and again appeared totally unruffled in a pressure situation.
Rain which had been hovering around the Cooper Associates County Ground began at 4pm, with 11 runs added in the final session and the stand between Imam and Rew worth 59.
Play resumed 40 minutes later and Price struck a huge blow for Gloucestershire when Imam edged a defensive push to give Bracey his third catch.
Three balls later Gregory fell to a catch at second slip to give Tom Price his fourth wicket. Brother Ollie, who took the catch, then spilled Rew off Payne.
Kasey Aldridge dragged a ball from Josh Shaw onto his stumps and at 163 for seven, Somerset were still 31 runs short of avoiding the possibility of being asked to bat again.
Davey became another Bracey victim, edging a drive off Dale. But, with the floodlights on, Rew remained unfazed and moved to 28 when notching the boundary to third-man that meant the follow-on was averted.
Sajid Khan survived a chance to Harris at third slip as Tom Price was denied a fifth victim and Somerset gained a precious batting point.