Head Covering — Eruvin 84b
רש"י עירובין דף פד/ב
בכומתא וסודרא - שרגילין לתת עליו בחול כובעין ומעפורת ותשמיש נוחה והגון הוא להם למשאוי קל כזה, ואף על פי שאינו ראוי להם לכיתוף משאוי כבד, כדאמר ביציאות השבת (שבת ח, א) תשעה מזו לכתף, אבל עשרה לא הני מילי למשאוי כבד, ומיהו, לכומתא וסודרא חזו, כשבני אדם עייפין בימות החמה נוטלין כובעיהן וסודרן מראשיהן עד שיפוח הרוח בהן
Rashi writes that when people are tired in the summer, they remove their hats and their yarmulkes from their heads until the breeze cools them down.
The Bechor Shor (to Shabbos 118b), who is very meticulous when it comes to men covering their heads, writes that when a person does remove his head coverings for this purpose, he should not walk more than four amos.
Accordingly, writes Teshuvos Minchas Yitzchok (), we can understand the ruling of the Beis Yosef (Orach Chaim §301), that a person may wear a hat out in a public thoroughfare on Shabbos even if it sits loosely on his head. This would seem difficult — why are we not concerned that it will blow off his head and that he might come to carry it four amos in the reshus ha'rabbim? However, according to the Bechor Shor, this is not a concern, because people are careful not to go four amos bareheaded — even though most authorities hold that covering the head is only a "trait of the pious" (מדת חסידות).
In fact, Teshuvos Shevus Yaakov (3:5; cited in Gilyonei HaShas here) proves that even in the Beis HaMikdash there was no prohibition to go bareheaded. Hence, when the Shulchan Aruch (Orach Chaim 91:3) writes that one must protest against a person who enters a Shul bareheaded, that is only a function of middas chassidus. Indeed, Teshuvos Maharshal (§72) proves that there is technically no prohibition to read the Shema and daven bareheaded (although the Taz, Orach Chaim §8, disputes this, asserting that it is prohibited because it mimics the practice of non-Jews [בחוקותיהם לא תלכו]). Therefore, if a duke or great ruler visits a Shul, and for some reason could not be convinced that Jews do not remove their head coverings in Shuls, and that their leaving them in place evinces no lack of respect — it is permitted to remove one's head covering even in the Shul.