How many times a day do you find yourself trying to quickly remember yet another 6-digit code sent via SMS and quickly type it as part of logging in to various sites and services? This is one of those situations where a computer should be more helpful - after all, it KNOWS the code it just received! Why make you memorize it and then type it again?
Took me a lot of Googling to find this solution. While not perfect, it's workable, and I use it daily. Hat tip to "A Better Way to Copy Two-Factor Codes on macOS"
Add this to your .zprofile
2fa(){ sqlite3 "$HOME/Library/Messages/chat.db" "select text from message order by date desc limit 1" | grep -oE '[0-9]{2,}' | pbcopy }
Note that Terminal must be granted Full Disk Access.
As you can see, this sequence of commands queries your Messages database for the last text message received, then grabs a sequence of numbers from it, and places it on Mac's clipboard, ready for you to paste.
To use, just issue 2fa command (you can certainly call it anything else) in Terminal after receiving that SMS, and - voila! - the code is ready to be pasted (command-V).
Alternatively, place the above command in .command file, do chmod +x, and drag it to the Dock (to the right of the separator). Then just click it once to execute.