bash: testing if string is a number | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

bash: testing if string is a number

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 07-19-2006
eur0dad eur0dad is offline
Registered User
 
Join Date: Jul 2006
Last Activity: 15 June 2007, 2:07 PM EDT
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
bash: testing if string is a number

How do you test if a string is a number?

Trying to do something like this:

x="AS"
if( x is not a number ); then
x=0
fi

Because I want to do number arithmetic with x.
Sponsored Links
    #2  
Old 07-19-2006
reborg's Avatar
reborg reborg is offline Forum Advisor  
Administrator Emeritus
 
Join Date: Mar 2005
Last Activity: 29 March 2012, 7:00 PM EDT
Location: Ireland
Posts: 4,464
Thanks: 0
Thanked 11 Times in 11 Posts
ok I guess it's an error if it's not numeric.

Why not :

typeset -i x before assigning it?
Sponsored Links
    #3  
Old 07-19-2006
Doc_RuNNeR Doc_RuNNeR is offline
Registered User
 
Join Date: Jun 2006
Last Activity: 23 September 2008, 11:23 PM EDT
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
Below I write a code with tow output the first one is to numeric arguments and the second if it isnīt numeric

Code:
#!/bin/bash
if echo $1 | grep "^[0-9]*$">aux
then
  echo "La cadena es un valor numerico."
else
  echo "La cadena no es un valor numerico."
fi
rm aux

Enjoy that, and please correct my english
    #4  
Old 07-20-2006
vino's Avatar
vino vino is offline Forum Advisor  
Supporter (in vino veritas)
 
Join Date: Feb 2005
Last Activity: 9 April 2013, 8:19 AM EDT
Location: Sydney, Down Under
Posts: 2,847
Thanks: 0
Thanked 12 Times in 12 Posts
If only you had searched the forums....

How to check whether a string is number or not
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Testing the length of a string intelinside Shell Programming and Scripting 1 04-15-2012 11:55 AM
Testing for non-zero length string longjon UNIX for Dummies Questions & Answers 5 12-08-2011 07:14 PM
changing number in bash (number is in form of string) Learnerabc Shell Programming and Scripting 3 11-02-2010 06:53 PM
Number/string format in bash josegr Shell Programming and Scripting 3 04-16-2009 02:24 PM
Testing the last character in a string dbrundrett Shell Programming and Scripting 11 07-15-2004 01:15 AM



All times are GMT -4. The time now is 12:35 AM.