How do I divide in the Linux console?

后端 未结 12 1935
情话喂你
情话喂你 2020-12-25 10:41

I have to variables and I want to find the value of one divided by the other. What commands should I use to do this?

相关标签:
12条回答
  • 2020-12-25 11:02

    Something else you could do using raytrace's answer. You could use the stdout of another shell call using backticks to then do some calculations. For instance I wanted to know the file size of the top 100 lines from a couple of files. The original size from wc -c is in bytes, I want to know kilobytes. Here's what I did:

    echo `cat * | head -n 100 | wc -c` / 1024 | bc -l
    
    0 讨论(0)
  • 2020-12-25 11:03

    Example of integer division using bash to divide $a by $b:

    echo $((a/b))
    
    0 讨论(0)
  • 2020-12-25 11:04

    you can also use perl -e

    perl -e 'print 67/8'
    
    0 讨论(0)
  • 2020-12-25 11:11

    I assume that by Linux console you mean Bash.

    If X and Y are your variables, $(($X / $Y)) returns what you ask for.

    0 讨论(0)
  • 2020-12-25 11:13
    echo 5/2 | bc -l
    

    2.50000000000000000000

    this '-l' option in 'bc' allows floating results

    0 讨论(0)
  • 2020-12-25 11:16

    You should try to use:

    echo "scale=4;$variablename/3"|bc
    
    0 讨论(0)
提交回复
热议问题