Removing leading zeros before passing a shell variable to another command

前端 未结 14 1308
长情又很酷
长情又很酷 2020-12-08 18:52

It turns out that iptables doesn\'t handle leading zeros too well. As $machinenumber that is used has to have a leading zero in it for other purposes, the idea

14条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-08 19:32

    I also can't comment or vote up yet, but the Duncan Irvine answer is the best.

    I'd like to add a note about portability. The $((10#0009)) syntax is not portable. It works in bash and ksh, but not in dash:

    $ echo $((10#09))
    dash: 1: arithmetic expression: expecting EOF: "10#09"
    
    $ dpkg -s dash | grep -i version
    Version: 0.5.7-2ubuntu2
    

    If portability is important to you, use the sed answer.

提交回复
热议问题