How can I get just the number from the variable contain numbers and characters??
for example: card=3Hearts
How can I get just the number (3) from the variable $card ?
How can I get just the number from the variable contain numbers and characters??
for example: card=3Hearts
How can I get just the number (3) from the variable $card ?
It's slight overkill for this particular problem, but expr
is very powerful this sort of matching problem:
expr $card : '[^0-9]*\([0-9]*\)'
and if you want to get that into another variable:
t=`expr $card : '[^0-9]*\([0-9]*\)'`
The expr
command can do a variety of things, but this colon operator – which matches the first argument against a regular expression – has lots of uses.