Reputation: 423
I want to check if $1
has any of the following special characters @#$%&*-=+
User will run the script like this:
testserver@matrix:~> scriptname somer@nd$m
script should detect if somer@nd$m
has any special character!
Thanks
Upvotes: 1
Views: 5790
Reputation: 77137
In general, to determine if a variable contains a member of a set of characters, you can use Pattern Matching with a character set as described in the bash manual.
[[ $var = *[set]* ]]
In your case, the set composition is tricky. It contains "&" and "-", which have special meaning. The dash goes first, or the shell considers the character set a range. The ampersand just has to be escaped. So you have to do
[[ $var = *[-@#$%'&'*=+]* ]]
Upvotes: 4
Reputation: 81
If you're looking for just the existence of a special characters you can use
if egrep -q "[@#$%&*-=+]" <<< "$1"
then
echo "Special Characters"
fi
Be aware though in your example that passing in somer@nd$m
will try to find a variable named m
unless you pass the argument in single quotes.
Upvotes: 0