Bash 正则表达式测试不起作用

Bash 正则表达式测试不起作用
echo "Enter username"
read $WORD

if [[ "$WORD" =~ ^(Dale|Paul|Ray)$ ]]; then
    echo "$WORD is valid"
else
    echo "$WORD is invalid"
fi

答案1

错误出现在read命令中,请使用read WORD而不是read $WORD

检查一下:

echo "Enter username"
read WORD

if [[ "$WORD" =~ ^(Dale|Paul|Ray)$ ]]; then
    echo "$WORD is valid"
else
    echo "$WORD is invalid"
fi

相关内容