Latest LQ Deal: Linux Power User Bundle
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
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!


  Search this Thread
Old 07-26-2016, 04:28 AM   #1
Registered: Feb 2011
Distribution: Centos 6.7, 7
Posts: 689

Rep: Reputation: 69
variables integer vs string

I'm reading the Advanced Bash Scripting Guide (, and I've come across the following:

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, 04:37 AM   #2
Registered: Feb 2011
Distribution: Centos 6.7, 7
Posts: 689

Original Poster
Rep: Reputation: 69
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, 05:31 AM   #3
LQ Guru
Registered: Mar 2004
Distribution: Slackware
Posts: 5,393

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


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

All times are GMT -5. The time now is 06:31 AM.

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