How to convert string to integer in UNIX

前端 未结 5 1191
广开言路
广开言路 2020-12-25 11:03

I have d1=\"11\" and d2=\"07\". I want to convert d1 and d2 to integers and perform d1-d2. How do I do this

5条回答
  •  攒了一身酷
    2020-12-25 11:26

    Any of these will work from the shell command line. bc is probably your most straight forward solution though.

    Using bc:

    $ echo "$d1 - $d2" | bc
    

    Using awk:

    $ echo $d1 $d2 | awk '{print $1 - $2}'
    

    Using perl:

    $ perl -E "say $d1 - $d2"
    

    Using Python:

    $ python -c "print $d1 - $d2"
    

    all return

    4
    

提交回复
热议问题