Last active
October 10, 2025 10:35
-
-
Save Enweave/a2ccd7ce3748334f5f4a07d3f0321d9c to your computer and use it in GitHub Desktop.
.bashrc, highlight russian homoglyphs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # homoglyphs are characters from different writing systems that look identical or nearly identical. | |
| # The Russian (Cyrillic) character "С" and the English (Latin) character "C" are a prime example. | |
| # which means, that if you type something like curl example.сom, where "C" in ".com" is Cyrillic "C" - it will obviously not work | |
| # Function below uses trap to catch those and highlight them | |
| # add it to your .bashrc or .bash_profile | |
| # Enable skipping commands from the DEBUG trap | |
| shopt -s extdebug | |
| # Define the homoglyph characters we want to check for | |
| HOMOGLYPHS_CYR="сСрРаАеЕоОхХНКМТВу" | |
| HOMOGLYPHS_LAT="cCpPaAeEoOxXHKMTBy" | |
| # Reentry guard for the DEBUG trap | |
| __HOMOGLYPH_IN_DEBUG="" | |
| function check_homoglyph_error() { | |
| # Interactive shells only | |
| [[ $- != *i* ]] && return 0 | |
| # Prevent recursion | |
| if [[ -n "$__HOMOGLYPH_IN_DEBUG" ]]; then | |
| return 0 | |
| fi | |
| __HOMOGLYPH_IN_DEBUG=1 | |
| local skip=0 | |
| if [[ -n "$BASH_COMMAND" ]]; then | |
| if echo "$BASH_COMMAND" | tr -d "$HOMOGLYPHS_LAT" | grep -q "[$HOMOGLYPHS_CYR]"; then | |
| local warning="\n⚠️ HOMOGLYPH DETECTED! ⚠️\n" | |
| warning+="It looks like you used a Russian (Cyrillic) character.\n" | |
| printf "%b" "$warning" | |
| if [[ -t 1 && "$(tput colors 2>/dev/null)" -ge 8 ]]; then | |
| local red_color no_color highlighted_command | |
| red_color="$(tput bold; tput setaf 1)" | |
| no_color="$(tput sgr0)" | |
| highlighted_command=$(sed -E "s/([${HOMOGLYPHS_CYR}])/${red_color}\1${no_color}/g" <<<"$BASH_COMMAND") | |
| printf 'Your command: %s\n' "$highlighted_command" | |
| else | |
| echo "Your command: $BASH_COMMAND" | |
| local indicator_line="" | |
| for (( i=0; i<${#BASH_COMMAND}; i++ )); do | |
| local char="${BASH_COMMAND:$i:1}" | |
| if [[ "$HOMOGLYPHS_CYR" == *"$char"* ]]; then | |
| indicator_line+="^" | |
| else | |
| indicator_line+=" " | |
| fi | |
| done | |
| echo " $indicator_line" | |
| fi | |
| # Ask whether to run anyway; default to "No" if no TTY or non‑y answer | |
| local ans="" | |
| if [[ -t 1 && -r /dev/tty ]]; then | |
| read -r -n 1 -p "Run it anyway? [y/N] " ans < /dev/tty | |
| echo | |
| [[ "$ans" =~ ^[Yy]$ ]] || skip=1 | |
| else | |
| skip=1 | |
| fi | |
| fi | |
| fi | |
| __HOMOGLYPH_IN_DEBUG="" | |
| if (( skip )); then | |
| # Prevent the command from running (requires extdebug) | |
| return 1 | |
| fi | |
| return 0 | |
| } | |
| # Run before every command | |
| trap 'check_homoglyph_error' DEBUG |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment