I have been working on this for a while, visiting dozens of sites and trying all kinds of combinations; however, I cannot get the script to run as intended. Even when they work in https://regex101.com/, I still cannot get them to work in bash.
I am trying to write a bash script which will validate that an input ("$password") is at least eight characters long and contains at least one number and at least one of these special characters: #?!@$ %^&*-
GNU bash, version 5.1.16(1)-release-(x86_64-pc-linux-gnu)
Any help would be greatly appreciated!
read -p "Please enter a password to test: " password
echo "You entered '$password'"
# I have tried all of the following (plus a few others) and cannot get it to work
#regex='^[a-zA-Z0-9#@$?]{8,}$'
#regex='^[a-zA-Z0-9@#$%&*+-=]{8,}$'
#regex='^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?[@#$%&*+-=]).{8,}$'
#regex='^(?=.*?[a-zA-Z0-9])(?=.*?[#?!@$ %^&*-]).{8,}$'
#regex='^(?=.*[A-Za-z])(?=.*[0-9])(?=.*[#?!@$ %^&*-]).{8,}$'
if [[ $password =~ $regex ]]; then
echo "This works"
else
echo "Nope"
fi
(?...)
constructs in particular are one common thing from Perl regexes that aren't present in POSIX regexes. – ilkkachu May 25 '22 at 15:57