LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 12-21-2007, 07:04 PM   #1
pgb205
Member
 
Registered: Nov 2007
Posts: 109

Rep: Reputation: 15
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.
 
Click here to see the post LQ members have rated as the most helpful post in this thread.
Old 12-21-2007, 07:12 PM   #2
Uncle_Theodore
Member
 
Registered: Dec 2007
Location: Charleston WV, USA
Distribution: Slackware 12.2, Arch Linux Amd64
Posts: 896

Rep: Reputation: 60
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~$
 
Old 12-21-2007, 07:12 PM   #3
anomie
Senior Member
 
Registered: Nov 2004
Location: Texas
Distribution: RHEL, Scientific Linux, Debian, Fedora, Lubuntu, FreeBSD
Posts: 3,930
Blog Entries: 5

Rep: Reputation: Disabled
This is covered in the Advanced Bash-Scripting Guide: 4.3. Bash Variables Are Untyped.
 
Old 12-21-2007, 08:01 PM   #4
jlinkels
Senior Member
 
Registered: Oct 2003
Location: Bonaire
Distribution: Debian Wheezy/Jessie/Sid, Linux Mint DE
Posts: 4,236

Rep: Reputation: 545Reputation: 545Reputation: 545Reputation: 545Reputation: 545Reputation: 545
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
 
Old 12-22-2007, 02:44 AM   #5
gnashley
Amigo developer
 
Registered: Dec 2003
Location: Germany
Distribution: Slackware
Posts: 4,775

Rep: Reputation: 481Reputation: 481Reputation: 481Reputation: 481Reputation: 481
Actually string comparisons should be quoted:

if [ "$this" = "$that" ]
then
fi
 
Old 12-22-2007, 10:14 AM   #6
jlinkels
Senior Member
 
Registered: Oct 2003
Location: Bonaire
Distribution: Debian Wheezy/Jessie/Sid, Linux Mint DE
Posts: 4,236

Rep: Reputation: 545Reputation: 545Reputation: 545Reputation: 545Reputation: 545Reputation: 545
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
 
Old 12-22-2007, 10:26 AM   #7
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
Quote:
Originally Posted by jlinkels View Post
BTW, you forgot a '=' in the comparison.
Actually for string comparison = and == are the same in bash
 
Old 12-22-2007, 12:18 PM   #8
gnashley
Amigo developer
 
Registered: Dec 2003
Location: Germany
Distribution: Slackware
Posts: 4,775

Rep: Reputation: 481Reputation: 481Reputation: 481Reputation: 481Reputation: 481
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.
 
Old 12-22-2007, 01:57 PM   #9
jlinkels
Senior Member
 
Registered: Oct 2003
Location: Bonaire
Distribution: Debian Wheezy/Jessie/Sid, Linux Mint DE
Posts: 4,236

Rep: Reputation: 545Reputation: 545Reputation: 545Reputation: 545Reputation: 545Reputation: 545
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
 
Old 12-25-2007, 07:42 AM   #10
AnanthaP
Member
 
Registered: Jul 2004
Location: Chennai, India
Distribution: UBUNTU 5.10 since Jul-18,2006 on Intel 820 DC
Posts: 659

Rep: Reputation: 160Reputation: 160
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
 
Old 12-25-2007, 02:52 PM   #11
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
Quote:
Originally Posted by AnanthaP View Post
a=expr'$a+0.000' gave someting like 760.0000
I'd like to know what magic shell are you running on!?!
 
Old 04-21-2010, 10:09 AM   #12
sqandr
LQ Newbie
 
Registered: Apr 2010
Posts: 1

Rep: Reputation: 0
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" Any feedback please?
 
Old 04-21-2010, 11:32 AM   #13
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
Quote:
Originally Posted by sqandr View Post
The output isn't a number, but the string "8-1" 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).
 
Old 04-21-2010, 11:40 AM   #14
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,563
Blog Entries: 29

Rep: Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179
Quote:
Originally Posted by sqandr View Post
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" 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!

Last edited by catkin; 04-21-2010 at 11:43 AM. Reason: tersley->tersely
 
Old 04-24-2010, 09:59 AM   #15
cola
Senior Member
 
Registered: Sep 2007
Location: Dhaka,Bangladesh
Distribution: Debian
Posts: 1,019

Rep: Reputation: 63
Quote:
Originally Posted by pgb205 View Post
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/
 
  


Reply

Tags
bash, conversion, convert, integer, string, type


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
converting string in integer in bash script dziadgba Linux - Newbie 5 08-31-2009 06:59 PM
C++ converting from char to integer MicahCarrick Programming 4 12-19-2005 03:16 PM
Integer To a String in C Bean101 Programming 2 05-27-2004 05:46 AM
converting integer value to hexadecimal string in C - any suggestions?? woodywellhung Programming 3 04-24-2004 06:27 PM
Converting integer types... JStew Programming 3 12-03-2002 09:53 AM


All times are GMT -5. The time now is 09:34 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration