You type passwords for sudo, ssh and databases into Terminal all day. One menu item makes it much harder for other software on the Mac to listen in.
Open the Terminal menu and choose Secure Keyboard Entry. A checkmark means it is on, and it stays on for every Terminal window, also after a restart.
Terminal (menu bar) > Secure Keyboard Entry
Apple describes it as preventing other apps on your computer or the network from detecting and recording what you type in Terminal. iTerm2 has the same setting in its own app menu.
I’d leave it on all the time. The cost is small, and it covers the moments you forget, like pasting a token or typing a database password by hand.
It does have side effects. Apps that rely on watching keystrokes can stop working while Terminal is in front, such as text expanders and some launchers. That is exactly what Apple warns about before you enable it. And it guards typing only. It does nothing about what appears on screen, what lands in your shell history or what is in the scrollback, so it is one layer, not the whole answer.
Further reading: Use secure keyboard entry in Terminal on Mac (Apple Support)