LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 07-26-2016, 03:28 AM   #1
vincix
Member
 
Registered: Feb 2011
Distribution: Centos 6.7, 7
Posts: 656

Rep: Reputation: 57
variables integer vs string


I'm reading the Advanced Bash Scripting Guide (http://tldp.org/LDP/abs/html/), and I've come across the following:
Code:
#!/bin/bash
#!/bin/bash
# int-or-string.sh

a=2334                   # Integer.
let "a += 1"
echo "a = $a "           # a = 2335
echo                     # Integer, still.


b=${a/23/BB}             # Substitute "BB" for "23".
                         # This transforms $b into a string.
echo "b = $b"            # b = BB35
declare -i b             # Declaring it an integer doesn't help.
echo "b = $b"            # b = BB35

let "b += 1"             # BB35 + 1
echo "b = $b"            # b = 1
echo                     # Bash sets the "integer value" of a string to 0.
Well, this simply doesn't work: b=${a/23/BB}
At least in Centos 7. If I try it, b has the same value as a. The /23/BB part is simply ignored. I've no idea why.
 
Old 07-26-2016, 03:37 AM   #2
vincix
Member
 
Registered: Feb 2011
Distribution: Centos 6.7, 7
Posts: 656

Original Poster
Rep: Reputation: 57
Yeah, it was my mistake. I simply wrote '22' instead of '23. So it's just a substitution. So when it finds '23' it replaces it with the second value. But only once.


What if I wanted to change it whenever it finds it?
 
Old 07-26-2016, 04:31 AM   #3
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 5,388

Rep: Reputation: 408Reputation: 408Reputation: 408Reputation: 408Reputation: 408
You would use double slashes syntax, b=${a//23/BB}
 
1 members found this post helpful.
  


Reply


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
bash scripting and integer variables. stf92 Linux - Software 1 06-06-2010 06:30 PM
[SOLVED] How to convert string to integer back to string C++ LazerPhreak Programming 1 03-13-2010 07:32 PM
Integer To a String in C Bean101 Programming 2 05-27-2004 04:46 AM
binding an integer to variables... xconspirisist Programming 4 11-05-2003 11:48 AM
length of integer variables daztheladd Programming 5 11-26-2002 07:29 AM


All times are GMT -5. The time now is 08:25 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration