Nick Haslam, the author of “Psychology in the Bathroom,” is a professor of psychology and head of the Melbourne School of Psychological Sciences at the University of Melbourne.

People protest the “bathroom bill” outside the North Carolina Executive Mansion in Raleigh, N.C. (AP Photo/Emery P. Dalesio)

Sigmund Freud didn’t think much of American public restrooms. During his 1909 visit to the United States, he grumbled that they lacked the refinement of European conveniences — when he could find one at all. Writing to a German friend years later, Freud’s lasting bitterness was obvious: “Is it not sad that we are materially dependent on these savages?”

It’s not surprising that a latrine could provoke such strong feelings: We’re seeing a version of these tensions play out today. This week, the White House ordered schools to provide trans students with bathrooms and locker rooms that match their gender identity. Meanwhile, lawmakers in states such as South Carolina, North Carolina, Tennessee and Virginia are trying to legislate which bathrooms transgender people can use, arguing that they should stick to facilities that match their birth certificate sex. Proponents argue that the laws are about privacy and public safety. “I think it’s just inappropriate,” said North Carolina state Sen. David Curtis (R), of trans people choosing a facility based on their gender identity. “We have rules in our society, and that’s just one of the rules.” Conservative blogger Matt Walsh put it more bluntly: “If you have a daughter in public school, you should certainly be concerned that boys now have a ‘civil right’ to follow your daughter into the locker room or the bathroom.”

These fears reflect both the vulnerability we feel in bathrooms and our expectation that these spaces are, and should be, strictly divided by sex. That’s nothing new. Public restrooms have always been riddled with anxiety and conflict. They’ve been sites of panic over contagious diseases, scandalous revelations about lewd behavior and political struggles over “potty parity” between men and women. The current controversy is only the latest saga.

One reason public restrooms provoke such strong reactions: They’re hotbeds of anxiety already. They’re places where private behavior becomes shared, where taboo subjects cannot be escaped, where intimate body parts are exposed. We’re taught from an early age that excretion should be secret, spoken of euphemistically, if at all. (Bathroom shame ran so high in the 1950s that CBS refused to air the pilot for “Leave It to Beaver” until the show was scrubbed of a shot of a closed toilet bowl.) As the psychologist Erik Erikson argued, childhood toilet accidents humiliate us, making us feel defective and infantile. In public facilities, then, we are violating something we learned early and deeply. Of course we feel awkward, embarrassed or vulnerable.

To understand how deeply that anxiety runs, look at how many people are unable to even use public bathrooms. A surprisingly large proportion of us — as high as 15 percent, according to some studies — suffer an aversion to public urination or defecation. For some, it’s so bad that sufferers remain housebound; others carefully plan their days around their excretory schedule.

Milder forms of bashful bladder are also common. In a 1976 investigation, researchers observed how long men stood at a urinal before urinating. It depended, they found, on the proximity of a fellow user. The closer the peer, the longer the wait.

Public restrooms also force us to confront the disgusting reality of others. Psychological studies have shown that people find their own fecal smells less offensive than those of others. People exposed to these smells — one study employed fart spray, a staple of pranksters — were harsher in their moral judgments. Gut feelings of revulsion turn into a primal rejection that can be transferred to other people in the vicinity. Public lavatories are places where disgust is rife and people are primed to distrust one another.

The fight over trans bathrooms is also explained by another tenet of bathroom psychology: Public restrooms, segregated by gender, make people highly aware of the sexual divide. Look at toilet graffiti, known among scholars as “latrinalia.” Men’s toilet scribblings are often graphic and tend to be sexual, aggressive, insulting and bigoted. Women’s tend to be longer, and more grammatical, romantic and supportive. Virtually unique to female restrooms is the tradition of extended exchanges of advice and encouragement. Sisterhood has its limits, though, as one well-punctuated Texas graffito reveals: “Keep him, Donna, you whore.”

Things get particularly interesting when gender and emotion intersect. Women are socialized to be more shame-prone than men, more mindful of being physically modest and substantially more likely to feel disgust. In bathroom settings, these tendencies translate into a greater concern with being overheard and “over-smelled.” The former worry can be remedied by a Japanese invention, marketed to women, that generates white noise to conceal shameful sounds.

These gender differences are exacerbated by a strong cultural double standard that renders femininity incompatible with excretion. “Women are supposed to be non-poopers,” in the words of one male participant interviewed for a study tastefully titled “Fecal Matters.” This sentiment is beautifully satirized in Jonathan Swift’s poem “The Lady’s Dressing Room,” in which a young suitor is aghast at the discovery that his beloved Celia has used a chamber pot. He recoils with horror: “Oh! Celia, Celia, Celia s—-!”

Despite our enlightened modern attitudes toward gender equality, women are still judged more severely for violations of the ideal of purity than men. In one study, a female experimenter who excused herself to use the bathroom was evaluated more negatively than one who excused herself to get some paperwork. No such difference was found for a male experimenter. Even our presidential politics are not immune from such concerns. During a commercial break at a Democratic debate in December, Hillary Clinton stepped offstage to use the bathroom. Donald Trump, speaking at a rally a few days later, told supporters: “I know where she went — it’s disgusting, I don’t want to talk about it. No, it’s too disgusting. Don’t say it, it’s disgusting.”

It makes sense, then, that the idea of a man in a women’s restroom would provoke a strong reaction. Women don’t want to expose their bodily functions to men, ever, and men don’t want them to. And men don’t want them to.

These tensions have played out in all kinds of ways. When President Franklin Roosevelt signed an executive order prohibiting racial discrimination in bathrooms, white female government workers staged a mass protest, fretting that they might catch venereal diseases if forced to share toilets with black women. One of the key talking points against the Equal Rights Amendment was that it might lead to co-ed bathrooms. “Do you want the sexes fully integrated like the races?” one hysterical pamphlet asked. When California, in 2013, passed a law allowing trans students to choose which school bathroom to use, Republican Assemblyman Tim Donnelly responded by pulling his 13-year-old son from public school.

Of course, it’s possible that Republican politicians are using these “bathroom bills” to wage a bigger battle against trans rights. If so, they’ve chosen a clever battleground, one that highlights our vulnerabilities and tweaks our belief that transgender people somehow unsettle the natural biological restroom divide. The first factor rests on the anxiety that pervades public restrooms: Exaggerated fears and invented dangers are its predictable products. The second is largely due to the salience of gender-segregated restrooms. The idea that bathrooms are single-sex is so ingrained that any shift in the social order makes us nervous.

By focusing our basic fears and making the gender divide so conspicuous, bathrooms are lightning rods for the sorts of hysteria we are now witnessing. Freud wouldn’t have been at all surprised.

On May 9, North Carolina Gov. Pat McCrory (R) and U.S. Attorney General Loretta Lynch announced dueling lawsuits regarding HB2, a law that requires people to use public restrooms according to the sex they were assigned at birth, rather than the one they identify with. (Monica Akhtar/The Washington Post)