Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

BerGaur

macrumors regular
Original poster
Dec 12, 2011
108
0
Inside my head
I needed to remove all the leading zeros in a script, so when I looked this up on line I found this solution that works so perfectly:

bash ~$ cat > Numbers.txt
000943
010000
000002
006500
bash ~$ OldNumbers=`cat Numbers.txt`
bash ~$ let NewNumbers=10#$OldNumbers
bash ~$ echo $NewNumbers > NewNumbers.txt
bash ~$ cat NewNumbers.txt
943
10000
2
6500
bash ~$


Now I have found that this # operator seems to multiply %10 of $OldNumbers by its self. So %10x%10 is %100 percent, thus the numbers stay the same. I have also found that you cannot use any numbers below 10, but you can use numbers above. 20#100 gives you 400. And I have found that I cannot exceed 64. The only connection I can think of is the fact that Mac OS X is 64 bit. Does anyone know what the # Arithmetic Operator does in Bash?
 
Last edited:
RE: bash pattern matchings...

Hi BerGaur,

In bash, the #, ##, %, %%, /, and // strings have a special use when manipulating strings from variables. In particular, the following hold:

Code:
${variable#pattern}   --> if "pattern" matches the beginning of the "variable"'s
                              value, then delete the shortest part that matches and
                              return the remainder
${variable##pattern} --> if "pattern" matches the beginning of the "variable"'s
                              value, then delete the longest part that matches and
                              return the remainder
${variable%pattern}  --> if "pattern" matches the ending of the "variable"'s
                              value, then delete the shortest part that matches and
                              return the remainder
${variable%%pattern} --> if "pattern" matches the ending of the "variable"'s
                              value, then delete the longest part that matches and
                              return the remainder
${variable/pattern/string} --> this does string replacement, so the longest
                              part of the "variable"'s value matching "pattern" is
                              replaced by "string".  This command only replaces the
                              first instance of "pattern" in the "variable"'s value.
${variable//pattern/string} --> this does the same string replacement, but all
                              matches are replaced.  Combinations of these are
                              possible, so if "pattern" begins with a #, it must match
                              the beginning of the "variable"'s value, if "pattern"
                              begins with a % it must match the ending.

As you can see, these bash commands are uniquely designed to perform just the magical string manipulation tasks that you desire. These string manipulations are often employed when manipulating the directory/file names.

Regards,
Switon

Edit: You know, I should probably also mention that these characters also have other uses in bash, for instance the # character is a comment character in a shell script and at the start of an array name it gives the length of the array, so ${#arrayname} would return the length of the i-th element of the array "array name".
 
Last edited:
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.