LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
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, 06:04 PM   #1
pgb205
Member
 
Registered: Nov 2007
Posts: 129

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, 06:12 PM   #2
Uncle_Theodore
Member
 
Registered: Dec 2007
Location: Charleston WV, USA
Distribution: Slackware 12.2, Arch Linux Amd64
Posts: 896

Rep: Reputation: 71
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, 06:12 PM   #3
anomie
Senior Member
 
Registered: Nov 2004
Location: Texas
Distribution: RHEL, Scientific Linux, Debian, Fedora
Posts: 3,935
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, 07:01 PM   #4
jlinkels
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195

Rep: Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043
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, 01:44 AM   #5
gnashley
Amigo developer
 
Registered: Dec 2003
Location: Germany
Distribution: Slackware
Posts: 4,928

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

if [ "$this" = "$that" ]
then
fi
 
Old 12-22-2007, 09:14 AM   #6
jlinkels
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195

Rep: Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043
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, 09:26 AM   #7
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
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, 11:18 AM   #8
gnashley
Amigo developer
 
Registered: Dec 2003
Location: Germany
Distribution: Slackware
Posts: 4,928

Rep: Reputation: 612Reputation: 612Reputation: 612Reputation: 612Reputation: 612Reputation: 612
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, 12:57 PM   #9
jlinkels
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195

Rep: Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043
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, 06:42 AM   #10
AnanthaP
Member
 
Registered: Jul 2004
Location: Chennai, India
Posts: 952

Rep: Reputation: 217Reputation: 217Reputation: 217
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, 01:52 PM   #11
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
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, 09: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, 10:32 AM   #13
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
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, 10:40 AM   #14
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
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 10:43 AM. Reason: tersley->tersely
 
Old 04-24-2010, 08:59 AM   #15
cola
Senior Member
 
Registered: Sep 2007
Posts: 1,045

Rep: Reputation: 65
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



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 05:59 PM
C++ converting from char to integer MicahCarrick Programming 4 12-19-2005 02:16 PM
Integer To a String in C Bean101 Programming 2 05-27-2004 04:46 AM
converting integer value to hexadecimal string in C - any suggestions?? woodywellhung Programming 3 04-24-2004 05:27 PM
Converting integer types... JStew Programming 3 12-03-2002 08:53 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:02 PM.

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
Open Source Consulting | Domain Registration