# arithmetic operator for Bash

Discussion in 'macOS' started by BerGaur, Dec 15, 2012.

  1. BerGaur, Dec 15, 2012
    Last edited: Dec 16, 2012

    BerGaur macrumors regular

    BerGaur

    Joined:
    Dec 12, 2011
    Location:
    Inside my head
    #1
    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?
     
  2. switon, Dec 16, 2012
    Last edited: Dec 16, 2012

    switon macrumors 6502a

    Joined:
    Sep 10, 2012
    #2
    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".
     

Share This Page