You are right, though for a real gun and bullet, the difference may be too small to measure.
Before we do any physics, we can consider an extreme case to clarify our thinking. Suppose we have a very light `gun', let's say a thin but strong tube of brass, and a very heavy `bullet', such as a lead sphere just fitting inside the tube. We can imagine the tube as supported by light, frictionless wheels. If we set off an explosion between the end wall of the tube and the lead sphere, I think it's obvious that the brass cylinder will fly off, and the lead sphere will stay more or less where it is. Whereas if we clamp the `gun' in place, the lead sphere will be shot out of the end.
To analyse the physics, the bullet and gun will both experience the same force, equal to the product of the pressure of the hot gases generated by the explosion and the cross-sectional area of the gun barrel. In response to this force, both the gun and the barrel will accelerate according to Newton's second law, F = ma.
Usually the mass of the gun is much greater than the mass of the bullet, so the acceleration of the bullet is much greater than that of the gun.
As long as the bullet is within the barrel, it continues to accelerate due to the pressure of the hot gas behind it, but as soon as it emerges from the barrel, the hot gases can expand freely and the bullet does not accelerate further. The time the bullet spends in the barrel depends on the speed with which the bullet is moving towards the open end of the barrel and the speed with which the barrel itself is moving in the opposite direction due to recoil. For the very massive bullet of our example, the barrel moves itself away from the bullet before the bullet has picked up much speed. For a real bullet, recoil will mean that the bullet spends slightly less time in the barrel than it would otherwise have done, and hence it emerges at a slightly lower muzzle velocity, and hence will not travel as far. Guns are a lot more massive than bullets, though, so this effect may be very small.