LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Converting string to integer in BASH (https://www.linuxquestions.org/questions/programming-9/converting-string-to-integer-in-bash-608444/)

pgb205 12-21-2007 06:04 PM

Converting string to integer in BASH
 
lets say i want to convert a string "76" to integer
76. How would I do this in BASH?

I remember in Pascal I could do something like char ('7')+39
or some such, and that would convert between character and a number
corresponding to that character. Can't seem to be able to figure
this out in BASH. What I actually wanted to do was to compare
two numbers one of which I got from top command. I finally
figured out that I could compare two strings as well, so the problem
is solved. But it would be nice to know how to convert between
integers and strings.

thanks a bunch in advance.

Uncle_Theodore 12-21-2007 06:12 PM

I don't think you need to actually convert anything...

teddy@toshiba~$ a="76"
teddy@toshiba~$ echo $((a+3))
79
teddy@toshiba~$ echo $((a-12))
64
teddy@toshiba~$

anomie 12-21-2007 06:12 PM

This is covered in the Advanced Bash-Scripting Guide: 4.3. Bash Variables Are Untyped.

jlinkels 12-21-2007 07:01 PM

I am gonna spoil your day.

Code:

this=14
that=16

#String comparison:
if [ $this == $that ]
then
fi

#Integer comparison:
if [ $this -eq $that ]
then
fi

See, no conversions made, this is correct code.

Even if you get $this and $that passed as cmd line parameters. The advantage of the second comparison is that you can find out if one varibale is greater than the other:
Code:

if [ $this -gt $that ]
then
fi


gnashley 12-22-2007 01:44 AM

Actually string comparisons should be quoted:

if [ "$this" = "$that" ]
then
fi

jlinkels 12-22-2007 09:14 AM

Not necessarily. Quoting is needed when to avoid errors when strings contain certain characters. So it is better practice. But for strings containing [0-9a-zA-Z] non quoted comparision works fine. For the sake of clearness and to show the untypedness (?) I advertently omitted the quotes here, although I always use them in production code.

BTW, you forgot a '=' in the comparison. ;)

jlinkels

colucix 12-22-2007 09:26 AM

Quote:

Originally Posted by jlinkels (Post 2998963)
BTW, you forgot a '=' in the comparison. ;)

Actually for string comparison = and == are the same in bash ;)

gnashley 12-22-2007 11:18 AM

I purposely left out the extra '=' as it seems to be irrelevant to the purpose of the code. Also, I know you can get way with not quoting strings, but it is good practice -especially when using single brackets.
Something like this will usually give errors when using single brackets:
Code:

if [ $this = "that" ]
then
fi

but double brackets may not.

jlinkels 12-22-2007 12:57 PM

You made made open the Bash scripting guide, and you are right, the '=' and '==' operator are equal. Pun intended.

Quote:

Originally Posted by gnashly
Something like this will usually give errors when using single brackets:
if [ $this = "that" ]
then
fi

However, assuming that you really meant "that" and not "$that" (A constant and not a variable) it doesn't give errors in my version of bash which is 3.1.17. I am not sure about other versions, nor about about other shells. Quoting might be the safest way to go in most cases.

jlinkels

AnanthaP 12-25-2007 06:42 AM

a=expr'$a/1'

Not sure of the quotes, but this is what the OP wants.

I tried something like this once for some iteration and noted that adding 0.0 didn't work.

a=expr'$a+0.000' gave someting like 760.0000

End

colucix 12-25-2007 01:52 PM

Quote:

Originally Posted by AnanthaP (Post 3001102)
a=expr'$a+0.000' gave someting like 760.0000

I'd like to know what magic shell are you running on!?! :confused:

sqandr 04-21-2010 09:09 AM

Surely conversion isn't needed?
 
I'm on GNU bash, version 4.0.33(1)-release (i486-pc-linux-gnu)
In my shell script, I need to use the static number of cores available in the system minus one; I'm doing the following:
Code:

var=`grep -c 'core id' /proc/cpuinfo`
var=$var-1
echo $var

The output isn't a number, but the string "8-1" :confused: Any feedback please?

colucix 04-21-2010 10:32 AM

Quote:

Originally Posted by sqandr (Post 3942733)
The output isn't a number, but the string "8-1" :confused: Any feedback please?

Indeed, it is what you're asking to the shell since
Code:

var=$var-1
just does string concatenation. To perform an arithmetic operation you need an arithmetic operator! Post #2 in this thread provides the solution.

An aside note: please, don't resurrect old threads like this. Better to start a new one. By the way, the Advanced Bash Scripting Guide already have all the answers to your question (see specifically §4.3, chapter 13 and §8.3).

catkin 04-21-2010 10:40 AM

Quote:

Originally Posted by sqandr (Post 3942733)
I'm on GNU bash, version 4.0.33(1)-release (i486-pc-linux-gnu)
In my shell script, I need to use the static number of cores available in the system minus one; I'm doing the following:
Code:

var=`grep -c 'core id' /proc/cpuinfo`
var=$var-1
echo $var

The output isn't a number, but the string "8-1" :confused: Any feedback please?

The expression $var-1 is a string concatenation as you found out. To do arithmetic
Code:

let var=var-1
or
((var=var-1))

More tersley you could use
Code:

let var--
or
((var--))

but they are not as transparent to programmers who do not know languages with decrement operators.

EDIT: you beat me to it, colucix :)

EDIT2: I didn't realise we were resurrecting the dead! 2007 and all that!

cola 04-24-2010 08:59 AM

Quote:

Originally Posted by pgb205 (Post 2998461)
lets say i want to convert a string "76" to integer
76. How would I do this in BASH?

I remember in Pascal I could do something like char ('7')+39
or some such, and that would convert between character and a number
corresponding to that character. Can't seem to be able to figure
this out in BASH. What I actually wanted to do was to compare
two numbers one of which I got from top command. I finally
figured out that I could compare two strings as well, so the problem
is solved. But it would be nice to know how to convert between
integers and strings.

thanks a bunch in advance.

Data types are not declared for bash script.
http://www.tldp.org/LDP/abs/html/


All times are GMT -5. The time now is 03:48 AM.