Pronunciation of the letter "x" in Latin and related languages

Portuguese x nearly always sounds like sh. There was a kiddie host in brazil named Xuxa, which sounds like SHOO-sha. When the x is at the end of the word, an interesting thing happens, where it sounds like “sheesh”. Apex = Uh-PEH-sheesh.
*
Why* that is, I don’t know. It’s always fascinated me, even with the commonalities of some words, how the pronunciation between words in Portuguese and Spanish diverged drastically. This happened only around 700 years ago.