bc 命令

xie仗劍天涯發表於2017-03-17

bc命令是一種支援任意精度的互動執行的計算器語言。是Linux簡單的計算器,能進行進位制轉換與計算。能轉換的進位制包括十六進位制、十進位制、八進位制、二進位制等。可以使用的運算子號包括(+)加法、(-)減法、(*)乘法、(/)除法、(^)指數、(%)餘數

語法(選項)

-i:強制進入互動式模式; 
-l:定義使用的標準數學庫; 
-w:對POSIX bc的擴充套件給出警告資訊; 
-q:不列印正常的GNU bc環境資訊; 
-v:顯示指令版本資訊; 
-h:顯示指令的幫助資訊。

例子

執行浮點運算和一些高階函式:

[root@study ~]# echo "1.1234*5" | bc
5.6170

設定小數精確度

[root@study ~]# echo "scale=3;10/3" | bc
3.333

引數scale=3是將bc輸出結果的小數位設定為3位

進位制轉換

[root@study ~]# vim obase.sh
  1 #!/bin/bash
  2 
  3 abc=255
  4 echo "obase=2;$abc" | bc   # 將十進位制轉換為二進位制
  5 
  6 def=11110000
  7 echo "obase=10;ibase=2;$def" | bc  # 將二進位制轉換為十進位制
[root@study ~]# . obase.sh  
11111111
240

bc --help

[root@study ~]# bc --help
usage: bc [options] [file ...]
  -h  --help         print this usage and exit
  -i  --interactive  force interactive mode
  -l  --mathlib      use the predefined math routines
  -q  --quiet        don't print initial banner
  -s  --standard     non-standard bc constructs are errors
  -w  --warn         warn about non-standard bc constructs
  -v  --version      print version information and exit
[root@study ~]#

相關文章