England, who had slumped to 83 for five, were 171 for five at tea, with rain threatening to stop a prompt resumption of a fascinating day's play.
Opening batsman Hales, under pressure for his place after a modest return in England's series win in South Africa, was a Test-best 71 not out with Bairstow, on his Yorkshire home ground, unbeaten on 54.
The pair's so far unbroken sixth-wicket stand of 88 helped England recover after Shanaka's treble strike went a long way to justifying Sri Lanka captain Angelo Mathews's decision to field first.
Mathews won the toss in overcast conditions reminiscent of those at Headingley two years ago when his side won the second Test to seal their first series win in England.
England captain Alastair Cook, who started his innings needing just 36 more runs to become the first Englishman to score 10,000 in Tests, got off the mark with a trademark clip off his legs for four.
But the left-handed opener was caught behind for 16 going for a cover drive off a wide but full delivery from Shanaka.
Three deliveries later and still in the 21st over, Shanaka had the under-fire Nick Compton out for nought with first slip Lahiru Thirimanne holding a low catch.
Joe Root, England's recently crowned player of the year in all formats, also fell for a duck to Shanaka as he edged to gully.
Shanaka had taken three wickets for one run in eight balls, again proving the value of accurate medium pace in conditions offering some assistance, if not extravagant movement, for swing and seam bowling.
England were 57 for three at lunch.
Hales was then 38 not out, with James Vince in the uncomfortable position of still being on nought after 19 minutes at the crease on his Test debut.
After getting off the mark Vince, 25, whose elegant style has been compared to that of Michael Vaughan, struck two fours -- a cover-drive and straight drive- worthy of the former England skipper.
But Vince exited for nine when he edged a good length delivery, this time from Shaminda Eranga, to gully.
All-rounder Ben Stokes struck three fours but holed out tamely to mid-on off Nuwan Pradeep.
Hales, whose defence at Test level has been called into question, completed an admirable 112-ball fifty in the circumstances when he drove Dushmantha Chameera behind point for his 10th four.
Bairstow was given out lbw on 41 but successfully reviewed the decision and soon afterwards lofted spinner Rangana Herath for six.
Sri Lanka then reviewed a not out call in favour of the wicket-keeper when Herath's lbw appeal was rejected, with replays again favouring Bairstow.
Players from both sides wore back armbands in memory of all those killed by recent flooding and landslides in Sri Lanka. – AFP
Original source: Hales, Bairstow stand tall