Having been reduced to 33 for four before lunch, a 219-run fifth-wicket stand between Younus (111) and Shafiq (111 not out) made the South African bowlers toil in the sun after captain Graeme Smith had won the toss.
The hosts dismissed Younus shortly before the close to leave the match evenly poised.
"After the first test we had a lot of meetings to discuss how we should play test cricket," Younus told reporters. "It was tough again this morning, but after we got through the new ball, if it was there to be hit, we went for it.
"I encouraged Asad to do the same and I think that helped him to relax. The wicket is much better for batting, I was a bit lucky but I hung in there. The bowlers were talking a lot to me but I just kept smiling and trying to hit the bad ball."
Smith had spoken about a "dangerous" Pakistan side following their 211-run defeat in the first test and so it proved as Younus and Shafiq started watchfully before accelerating after tea.
Shafiq was dropped by Dean Elgar at short leg off spinner Robin Peterson when on just 24, but otherwise gave a chanceless performance in reaching his third test century off 201 balls including 13 fours and a six.
The experienced Younus reached his hundred from 192 balls with six fours and three towering sixes off Peterson. It was his 21st in tests and fourth against South Africa.
He was dismissed on review after an inside edge off Vernon Philander carried to wicketkeeper AB de Villiers.
"I think if we can get through the morning session tomorrow and post 350 or 400 then we have a good chance to win the test," Younus said.
Morne Morkel was the pick of the bowlers with two for 37 from his 16 overs and Philander took two for 45. Dale Steyn deserved more than his one for 34 as he went past the outside edge on numerous occasions.
"Newlands always offers a bit in the morning, so hopefully with a new ball that is only 10 overs old we can get through them quickly," Philander said
It looked like it was going to be another batting disaster for Pakistan early on.
Nasir Jamshed (3) offered a catch to De Villiers with a loose front-foot drive off Philander, Mohammad Hafeez (17) tentatively pushing forward to a full ball from Steyn and Smith pouched the catch at first slip.
Morkel had Azhar Ali (4) caught behind by De Villiers and four balls later Pakistan captain Misbah-ul-Haq fended a rising delivery into his ribs with his gloves and Elgar took the catch at short leg.
South Africa lead the three-match series 1-0 after their victory in Johannesburg.