[Bash] How to extract number from string in bash shell

36

I will list some ways to extract number from string in bash shell script. We will set two sample variables to use with commands for extracting.Gnu-bash-logo

VAR1=”Im 25 years old.”
VAR2=”Im 25 years old. I have worked as system admin for 5 years.”

Four ways to extract number from string in bash shell


1. ‘tr’ command

 


2. ‘sed’ command


3. bash built-in

or

4. ‘grep’ command

 
May be we have more than ways better to extract number from string, if you know , please comment to share with me. Thank you.

Clip demo

LEAVE A REPLY