My issues is that I want to read a character from the stdin and compare it with a string value. If that character exists then I want to display a message. Eg, saving % into the variable $a:

$ read a
$echo $a

There is variable defined say v='~`!@#$%^&*()_-+=:;{[}]|\/<>,."' all possible special character including single tick '

I need to check if the character input exists in the string value. How can I do this.

case $v in
  *"$a"*) printf '<%s> is in <%s>\n' "$a" "$v"
|improve this answer|||||

Here you go:

echo type any character and press enter
IFS= read -r a
[[ $v =~ "$a" ]] && echo match || echo not
|improve this answer|||||
  • Janos, I didn't know how to run this from the script: [[ $v =~ $a ]] && echo match || echo not – Nirmal Arri Jan 16 '14 at 22:31
  • @NirmalArri save the script for example in script.sh, and run it with bash script.sh – janos Jan 16 '14 at 22:52

Try this

cat | egrep -e '[~`!@#$%^&*()_-+=:;{[}]|/<>,."]'
|improve this answer|||||
  • What are you cating here? Did you mean something like echo "$a" | grep -E ["$v"]? – terdon Jan 16 '14 at 23:22
  • 1
    No, not quite: the first ] marks the end of the set. – Gilles 'SO- stop being evil' Jan 16 '14 at 23:48
  • @Giles You're correct hadn't noticed that! – X Tian Jan 17 '14 at 1:32
  • @terdon I just typed a couple of test lines straight into cat, but agree your use of variables closer to question. – X Tian Jan 17 '14 at 1:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.