There is nothing different about a cave that would make it immune to the shaking from an earthquake. Just as there are safer and less safe places to be on the surface of the earth during an earthquake, there are also various characteristics inside caves that make some cave locations safer or less safe than others. First of all, whether or not you feel an earthquake in a cave depends chiefly upon the magnitude or size of the earthquake and the distance from the earthquake source to the cave in question. The closer and larger the earthquake, the more shaking you’ll feel. The rest of the information about cave stability and shaking effects is based on limited observations and is a major area of active research.
The complexity of the cave seems to be a very important factor with regard to issues of cave passage "stability". A small tube-like passage appears to be a relatively safe location that doesn’t tend to collapse or sustain much, if any, damage from earthquake shaking. However, large cave passages or “rooms” are notably less stable places. It is in these areas where fallen chunks of limestone or marble are commonly observed, and where broken or toppled cave formations tend to be found.
Shaking effects inside caves include damage to delicate formations like soda straw stalactites that effectively “die” and stop growing. Sometimes stalagmites or columns can be toppled. Toppled or not, renewed growth on them can occur. These effects are far short of a total passage collapse, but collapse of portions of cave ceilings has been observed in caves, notably from caves in Missouri and Indiana near the New Madrid and Wabash seismic zones.
So are caves safe in earthquakes? Generally yes, but it depends on the cave characteristics and where you are in it.
An interesting note: Cavers who have experienced earthquakes while underground have described sounds like a distant aircraft passing by; becoming perceptibly louder, then fading away.