A generalized Reynolds analogy (GRA) is proposed for compressible wall-bounded turbulent flows (CWTFs) and validated by direct numerical simulations. By introducing a general recovery factor, a similarity between the Reynolds-averaged momentum and energy equations is established for the canonical CWTFs (i.e. pipes, channels, and flat-plate boundary layers that meet the quasi-one-dimensional flow approximation), independent of Prandtl number, wall temperature, Mach number, Reynolds number, and pressure gradient. This similarity and the relationships between temperature and velocity fields constitute the GRA. The GRA relationship between the mean temperature and the mean velocity takes the same quadratic form as Walz’s equation, with the adiabatic recovery factor replaced by the general recovery factor, and extends the validity of the latter to diabatic compressible turbulent boundary layers and channel/pipe flows. It also derives Duan & Martín’s (J. Fluid Mech., vol. 684, 2011, pp. 25–59) empirical relation for flows at different physical conditions (wall temperature, Mach number, enthalpy condition, surface catalysis, etc.). Several key parameters besides the general recovery factor emerge in the GRA. An effective turbulent Prandtl number is shown to be the reason for the parabolic profile of mean temperature versus mean velocity, and it approximates unity in the fully turbulent region. A dimensionless wall temperature, that we call the diabatic parameter, characterizes the wall-temperature effects in diabatic flows. The GRA also extends the analysis to the fluctuation fields. It recovers the modified strong Reynolds analogy proposed by Huang, Coleman & Bradshaw (J. Fluid Mech., vol. 305, 1995, pp. 185–218) and explains the variation of the temperature–velocity correlation coefficient with wall temperature. Thus, the GRA unveils a generalized similarity principle behind the complex nonlinear coupling between the thermal and velocity fields of CWTFs.