Removal of insignificant zeros from a variable

Solved
tbeghain Posted messages 63 Status Member -  
lEprofSonDkon Posted messages 227 Status Member -
Hello,

I'm following up on a similar request made a while ago. I'm experiencing the same issue (I want to remove the insignificant leading zeros from an alphanumeric variable that contains only digits).
I copied exactly the example that was given back then.
If I apply the recommended solution directly from the command line, it works.

>var=0000001253; echo ${var##*(0)}
1253


However, as soon as I put it into a shell script, I do not get the expected result at all:
>cat test.sh


#!/bin/bash var=0000001253; echo ${var##*(0)}


>test.sh
0000001253


Well, I'm on CentOS7 Linux. It must be something simple. Maybe I'm missing something....

Thanks for your replies

Configuration: CentOS Linux release 7.5.1804 (Core)

5 answers

  1. dubcek Posted messages 18627 Registration date   Status Contributor Last intervention   5 659
     
    $ cat test.sh
    #!/bin/bash
    shopt -s extglob
    var=0000001253; echo ${var##*(0)}
    $ test.sh
    1253
    $
    2
  2. mamiemando Posted messages 33228 Registration date   Status Moderator Last intervention   7 940
     
    Hello,

    Indeed it doesn’t work. You can remove the leading zeros with
    sed
    :

    #!/bin/bash var=0000001253 var=$(echo $var | sed -e "s/^0*//") echo $var


    Good luck
    1
    1. tbeghain Posted messages 63 Status Member 3
       
      Hello,

      Yes, that works. Thank you very much. But still, I would like to understand why the direct command isn't working in the script...
      0
  3. dubcek Posted messages 18627 Registration date   Status Contributor Last intervention   5 659
     
    hello
    do not execute the script by:
    sh test.sh
    0
    1. tbeghain Posted messages 63 Status Member 3
       
      Hello,

      Um!!! But how do I run the script then? In any case, I wasn't running it with sh test.sh, but just test.sh...
      0
  4. tbeghain Posted messages 63 Status Member 3
     
    Hello,

    Yep! It works with shopt too. Thanks a lot. I don't know what this shopt is. I'll look into it.
    0
    1. dubcek Posted messages 18627 Registration date   Status Contributor Last intervention   5 659
       
      in the bash man page, see
      Filename Expansion
      ....
      If the extglob shell option is enabled using the shopt command, several extended pattern matching operators are recognized. ...
      0
  5. lEprofSonDkon Posted messages 227 Status Member 13
     
    Hi,

    in bash:
    echo $((10#$var))
    0