How to filter password user input char-wise for special characters, suppressing ESC sequences in Bash?

I’m trying to find a solution which can filter user input for a password char-wise via read command (not by read -e, which prevents directly responding to key strokes), filtering and handling special characters in real-time. As first char, space or any special chars shouldn’t be allowed. How to achieve that, yet avoiding non-printable ESC-sequences (everything which moves the cursor, apart from backspace), which potentially would pollute the password input?

# Set of allowed chars
allowedDefaultChars='[a-zA-Z0-9]'
allowedSpecialChars='[-_ ()/~?!§,.#+]'
pw=
while true; do
    read -rsn 1
    if [[ $REPLY ]]; then
        ...
        pw+="$REPLY"
    fi
done
echo "$pw"

Through further investigation and experimentation, I’ve come to the following solution:

#! /bin/bash

# Validated password entry with filtered ESC sequences / disabled ESC and a set of allowed special chars / disallows special chars or space as 1st char
validate_password() {

    local pw
    local escKey=$'x1B'
    local backspaceKey=$'x7f'
    local allowedDefaultChars='[a-zA-Z0-9]'
    local allowedSpecialChars='[-_ ()/~?!§,.#+]'
    echo -e "n*** Password validation ***nAllowed special charset, unless 1st char is >> $allowedSpecialChars <<nnEnter password:" >/dev/tty
    local isFirstChar="1"
    while true; do
        read -rsn 1
        # Stop on ENTER
        if [[ ${#REPLY} -eq 0 ]]; then
            break
        fi
        # Filter ESC sequences / block ESC key
        if [[ $REPLY == $escKey ]]; then
            while true; do
                read -rsn1 -t 0.001
                if [[ ${#REPLY} -eq 0 ]]; then
                    break
                fi
            done
            continue
        fi
        # Block 1st char special char or space
        if [[ $isFirstChar ]]; then
            if [[ $REPLY != $allowedDefaultChars ]]; then
                echo -e "First char must not be a special char or space!e[1A" >/dev/tty
                continue
            fi
            # Clear and stay in message line
            echo -e "e[Ke[1A" >/dev/tty
            unset -v isFirstChar
        fi
        # Filter out disallowed chars for input
        if [[ $REPLY == $allowedDefaultChars || $REPLY == $allowedSpecialChars ]]; then 
            pw+="$REPLY" 
            printf '%s' '*' >/dev/tty
        # Backspace - delete last entered password char and a '*' from dispay
        elif [[ $REPLY == $backspaceKey ]]; then
            pw="${pw:0:-1}" 
            printf '%s' $'e[1De[0K' >/dev/tty
        fi
    done
    
    echo "$pw"
    unset pw
    echo -e >/dev/tty
}

echo -e "PW IS: $(validate_password)"

The function blocks any input of special chars or space as 1st char, showing a warning message. Upon entering the 1st valid char, all further chars from either valid set of allowedDefaultChars or allowedSpecialChars, are added to the local pw var, printing the next * on screen.

All visual output is redirected to >/dev/tty, so as not to pollute the password, if capturing the function call into a var.

If backspace is pressed, the nexte previously stored char, along with the its * representation on screen, is removed again.

All directly from keyboard accessible keys generating ESC-sequences / non-printable chars, should be successfully suppressed and filtered (ESC, F1-F12, arrow keys, home, end, pgup, pgdn, …), even on upon excessive, rapid multiple keypressing, without ‘leaked’ key sequences.

ENTER finishes the input.

I’ve also tested copy’n’paste input from KeepassXC, which seemed to work fine (unless the pasted password contains forbidden chars).

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật