Bash Integers/String


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Bash Integers/String
# 1  
Old 11-20-2013
Bash Integers/String

Hy friends, I am newbie to bash scripting, can anyone explain how
Code:
b=${a/23/BB} # Substitute "BB" for "23".

this line converts "b" into string and
Quote:
d=${c/BB/23} # Substitute "23" for "BB".
and "d" into Integer. Thanks in advance
# 2  
Old 11-20-2013
See "Shell parameter expansion" section of the bash manual:

http://www.gnu.org/software/bash/man...eter-Expansion
# 3  
Old 11-23-2013
I couldn't get that.. can you explain it here ?
# 4  
Old 11-23-2013
Hi Qazi...

You couldn't get it?
Code:
${parameter/pattern/string}

The pattern is expanded to produce a pattern just as in filename expansion. Parameter
is expanded and the longest match of pattern against its value is replaced with string.
If pattern begins with ‘/', all matches of pattern are replaced with string. Normally only
the first match is replaced. If pattern begins with ‘#', it must match at the beginning of
the expanded value of parameter. If pattern begins with ‘%', it must match at the end of
the expanded value of parameter. If string is null, matches of pattern are deleted and the
/ following pattern may be omitted. If parameter is ‘@' or ‘*', the substitution operation is
applied to each positional parameter in turn, and the expansion is the resultant list. If
parameter is an array variable subscripted with ‘@' or ‘*', the substitution operation is
applied to each member of the array in turn, and the expansion is the resultant list.

And BTW, just because you see characters representing a number does not mean it is an integer, (or float).
In the case of bash everything is a string and bash decides how to interpret strings according to how you code for them.
Example:-
Code:
Last login: Sat Nov 23 11:02:56 on ttys000
AMIGA:barrywalker~> echo -n 1234567890 > /tmp/string.txt
AMIGA:barrywalker~> hexdump -C < /tmp/string.txt
00000000  31 32 33 34 35 36 37 38  39 30                    |1234567890|
0000000a
AMIGA:barrywalker~> echo -n 1234567890.0987654321 > /tmp/string.txt
AMIGA:barrywalker~> hexdump -C < /tmp/string.txt
00000000  31 32 33 34 35 36 37 38  39 30 2e 30 39 38 37 36  |1234567890.09876|
00000010  35 34 33 32 31                                    |54321|
00000015
AMIGA:barrywalker~> _

# 5  
Old 11-23-2013
Code:
b=${a/23/BB}

this works on variable "a" and replaces "23" with "BB".

Code:
root@OpenWrt:~# a=1234
root@OpenWrt:~# echo ${a/23/BB}
1BB4

It's a simple substitution, only working on the first match since only one slash

Code:
root@OpenWrt:~# a=12341234
root@OpenWrt:~# echo ${a/23/BB}
1BB41234
root@OpenWrt:~# echo ${a//23/BB}
1BB41BB4

Clear as mud now? Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Escape bash-special character in a bash string

Hi, I am new in bash scripting. In my work, I provide support to several users and when I connect to their computers I use the same admin and password, so I am trying to create a script that will only ask me for the IP address and then connect to the computer without having me to type the user... (5 Replies)
Discussion started by: arcoa05
5 Replies

2. Shell Programming and Scripting

Comparing Integers (I think)

Hi, I can't figure out what I'm missing. I'm running a query to see if there are any streams recording on my DVR before starting a scripted update. I'm guessing that it is viewing $VIDEO as a string instead of an int. I've tried everything I saw on google but it still comes back as $VIDEO is... (8 Replies)
Discussion started by: Rhysers
8 Replies

3. UNIX for Dummies Questions & Answers

Strings to integers?

Hi, I'm totally new at this, so help will be appreciated. I have a directory with a bunch of files in it. The files are named xinteger_yinteger_zinteger.vtk (eg, x3_y0_z-1.vtk). I want to read the filenames and then assign the integers to variables that I then can use in expressions. So, for... (6 Replies)
Discussion started by: jhsinger
6 Replies

4. Shell Programming and Scripting

Grep float/integers but skip some integers

Hi, I am working in bash in Mac OSX, I have following 'input.txt' file: <INFO> HypoTestTool: >>> Done running HypoTestInverter on the workspace combined <INFO> HypoTestTool: The computed upper limit is: 11 +/- 1.02651 <INFO> HypoTestTool: expected limit (median) 11 <INFO> HypoTestTool: ... (13 Replies)
Discussion started by: Asif Siddique
13 Replies

5. Shell Programming and Scripting

Cancel down 2 integers

Wonderful evening to all of you! My problem has to possible starting points. Well, not really, but getting to either one is no problem at all. So i got either a string in the format of "1920x1080" or simply the integers X = 1920 and Y = 1080. When I am done, I would like to have an output... (5 Replies)
Discussion started by: jakunar
5 Replies

6. Shell Programming and Scripting

Editing lists of integers in 1d files with bash shell

Hi, I need a script that will: 1. Go through about 20 different folders, each containing about 20 1d files. The 1d files go something like this: 22.253 37.707 78.117 112.374 127.944 156.067 180.956 233.785 249.256 ... (1 Reply)
Discussion started by: ac130pilot
1 Replies

7. Shell Programming and Scripting

Add non-integers using ksh

I would like to add 4.7 and 1.2. However I am unable to do this with expr. Any simple ideas (even using something other than expr)? Example: me> expr 4 + 1 5 me> expr 4.7 + 1.2 expr: 0402-046 A specified operator requires numeric parameters. (18 Replies)
Discussion started by: 2dumb
18 Replies

8. Shell Programming and Scripting

bash problem when comparing integers

Hi, When I compare currentfiledate to reportfiledate I get a "-bash: currentfiledate=20090220080101 reportfiledate=20090219231245 if ; then echo -ne "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! \n" echo -ne "!! Running the load utility !! \n" fi Our... (15 Replies)
Discussion started by: rbournival
15 Replies

9. Programming

Using write() with integers in C

I'm trying to write an integer to a file using the write() function, but write() requires the parameter to be written to be a const void*. How would I go about doing this? also: using itoa() produces a " warning: implicit declaration of function 'itoa' " even though i have #included stdlib.h (2 Replies)
Discussion started by: h@run
2 Replies

10. Shell Programming and Scripting

integers in the if statement

hi, im trying to compare two variables in csh to put in an if statement, eg: set a = $firstnum set b = $secondnum if ($a -ge $b) echo $a But I get an error ("if: Expression syntax"). How can I make csh see my variables as integers? thanks in advance! (5 Replies)
Discussion started by: Deanne
5 Replies
Login or Register to Ask a Question