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

  Unix/Linux 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 Linux or Unix Question    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 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 Unix and Linux 07-19-2006
reborg's Unix or Linux Image
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 Unix and Linux 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 Unix or Linux Image
    #4  
Old Unix and Linux 07-19-2006
vino's Unix or Linux Image
vino vino is offline Forum Advisor  
Supporter (in vino veritas)
 
Join Date: Feb 2005
Last Activity: 12 March 2015, 5:33 AM EDT
Location: Sydney, Down Under
Posts: 2,848
Thanks: 0
Thanked 13 Times in 13 Posts
If only you had searched the forums.... Unix or Linux Image

How to check whether a string is number or not
Sponsored Links
Closed Linux or Unix Question

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Unix or Linux Image 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 10:55 AM
Testing for non-zero length string longjon UNIX for Dummies Questions & Answers 5 12-08-2011 06:14 PM
changing number in bash (number is in form of string) Learnerabc Shell Programming and Scripting 3 11-02-2010 05:53 PM
Number/string format in bash josegr Shell Programming and Scripting 3 04-16-2009 01:24 PM
Testing the last character in a string dbrundrett Shell Programming and Scripting 11 07-15-2004 12:15 AM



All times are GMT -4. The time now is 12:34 PM.