ZrSiS is a nodal-line semimetal, whose electronic band structure contains a diamond-shaped line of Dirac nodes. We carried out a comparative study on the optical conductivity of ZrSiS and related compounds ZrSiSe, ZrSiTe, ZrGeS, and ZrGeTe by reflectivity measurements over a broad frequency range combined with density functional theory calculations. The optical conductivity exhibits a distinct U shape, ending at a sharp peak at around 10000~cm$^{-1}$ for all studied compounds, except for ZrSiTe. The U shape of the optical conductivity is due to transitions between the linearly dispersing bands crossing each other along the nodal line. The sharp high-energy peak is related to transitions between almost parallel bands, and its energy position depends on the interlayer bonding correlated with the $c$/$a$ ratio, which can be tuned by either chemical or external pressure. For ZrSiTe, another pair of crossing bands appears in the vicinity of the Fermi level, corrugating the nodal-line electronic structure and leading to the observed difference in optical conductivity. The findings suggest that the Dirac physics in Zr$XY$ compounds with $X$=Si, Ge and $Y$=S, Se, Te is closely connected to the interlayer bonding.