book
  • README
  • cheatsheet
    • bash
      • builtin
      • syntactic sugar
      • cmd
      • havefun
    • text-processing
      • awk
      • sed
      • html
      • json
      • regex
      • unicode
    • osx
    • curl
    • tricky
    • widget
    • proxy
    • colors
    • math
    • media
    • ssl
      • keystore
      • verification
      • server
      • client
      • tricky
    • windows
      • powershell
      • choco
      • wsl
      • wt
      • shortcut
      • clsid
      • env
      • shell:folder
  • vim
    • nvim
    • install
    • color
    • plugins
      • usage
      • other plugins
      • deprecated
    • tricky
    • viml
    • windows
    • troubleshooting
  • devops
    • admin tools
    • ssh
    • git
      • config
      • alias
      • submodule
      • eol
      • example
      • gerrit
        • gerrit API
      • github
      • troubleshooting
      • tricky
      • statistics
    • pre-commit
    • release-tools
    • tmux
      • cheatsheet
    • ansible
    • vault
    • artifactory
      • api
      • cli
      • aql
      • nginx cert
    • klocwork
      • kwadmin
      • kwserver
      • api
      • q&a
    • elk
    • mongodb
    • android
    • mobile
  • jenkins
    • config
      • windows
    • appearance
    • troubleshooting
    • jenkinsfile
      • utility
      • parallel
      • build
      • envvar
      • properties
      • trigger
      • node
    • script
      • job
      • build
      • stage
      • agent
      • security & authorization
      • exception
      • monitor
      • tricky
    • api
      • blueocean
    • cli
    • plugins
      • kubernetes
      • docker
      • shared-libs
      • lockable-resource
      • ansicolor
      • badge
      • groovy-postbuild
      • simple-theme
      • customizable-header
      • artifactory
      • jira-steps
      • job-dsl
      • build-timeline
      • crumbIssuer
      • coverage
      • uno-choice
      • tricky
  • virtualization
    • kubernetes
      • init
        • kubespray
        • kubeadm
          • environment
          • crio v1.30.4
          • docker v1.15.3
          • HA
        • addons
        • etcd
      • kubectl
        • pod
        • deploy
        • replicasets
        • namespace
        • secrets
      • node
      • certificates
      • events
      • kubeconfig
      • kubelet
      • troubleshooting
      • cheatsheet
      • auth
      • api
      • tools
        • monitor
        • helm
        • network
        • minikube
    • docker
      • run & exec
      • voume
      • remove
      • show info
      • dockerfile
      • dockerd
      • tricky
      • troubleshooting
      • windows
    • crio
    • podman
  • ai
    • prompt
  • osx
    • apps
      • init
      • brew
    • defaults
    • system
    • network
    • script
    • tricky
  • linux
    • devenv
    • util
      • time & date
      • output formatting
      • params
      • tricky
    • nutshell
    • disk
    • network
    • troubleshooting
    • system
      • apt/yum/snap
      • authorization
      • apps
      • x11
    • ubuntu
      • systemctl
      • x
    • rpi
  • programming
    • groovy
    • python
      • config
      • basic
      • list
      • pip
      • q&a
    • others
    • archive
      • angular
      • maven
      • mysql
        • installation
        • logs
      • ruby
        • rubyInstallationQ&A
  • tools
    • fonts
    • html & css
    • Jira & Confluence
    • node & npm
      • gitbook
      • hexo
      • github.page
      • code themes
    • app
      • microsoft office
      • vscode
      • virtualbox
      • iterm2
      • browser
      • skype
      • teamviewer
      • others
  • quotes
  • english
Powered by GitBook
On this page
  • sum
  • sum from file
  • number conversion
  • advanced computing

Was this helpful?

  1. cheatsheet

math

PreviouscolorsNextmedia

Last updated 1 month ago

Was this helpful?

[!NOTE|label:references:]

sum

[!INFO|label:references:]

awk

$ seq 10 | awk '{s+=$1} END {print s}'
55

# or
$ awk 'BEGIN{print '"1+2+3"'}'
6

file sizes

$ ls -l
total 12
-rw-r--r--  1 marslo staff 3480 Dec 21 21:21 README.md
-rw-r--r--  1 marslo staff 7516 Dec 21 21:21 SUMMARY.md
drwxr-xr-x  7 marslo staff  224 Sep 19 19:52 artifactory
drwxr-xr-x 15 marslo staff  480 Dec 21 21:19 cheatsheet
drwxr-xr-x 12 marslo staff  384 Aug 17 21:11 devops
drwxr-xr-x  9 marslo staff  288 Sep 29 17:31 jenkins
drwxr-xr-x 10 marslo staff  320 Sep 19 19:52 linux
drwxr-xr-x  9 marslo staff  288 Sep 19 19:52 osx
drwxr-xr-x  6 marslo staff  192 Aug 17 21:11 programming
drwxr-xr-x 27 marslo staff  864 Aug 17 22:10 screenshot
drwxr-xr-x  7 marslo staff  224 Oct 11 19:41 tools
drwxr-xr-x  8 marslo staff  256 Aug 30 16:39 vim
drwxr-xr-x  5 marslo staff  160 Aug 17 21:11 virtualization

$ ls -l | awk '{sum += $5} END {print sum}'
14676

datamash

[!TIP|label:reference:]

$ seq 10 | datamash sum 1
55

bc

  • $ seq 10 | paste -sd+ -
    1+2+3+4+5+6+7+8+9+10
    
    $ seq 10 | paste -sd+ - | bc
    55
  • $ seq 10 | xargs printf "- - %s" | xargs | bc
    55
  • $ seq 10 | sed 's/^/.+/' | bc
    1
    3
    6
    10
    15
    21
    28
    36
    45
    55
$ seq 10 | jq -s 'add'
55
$ f=$(seq 10)
$ echo $(( ${f//$'\n'/+} ))
55

# or
$ echo $(( $(seq 10 | tr "\n" "+") 0 ))
55
# or from file
$ echo $(( $( tr "\n" "+"  < /tmp/test) 0 ))

sum from file

[!NOTE|label:sample file:]

$ cat numbers.txt
73.27
218.38
14.15
9.18
16.60
  • 
    $ awk '{ sum += $1 } END { print sum }' numbers.txt
    331.58
  • $ paste -sd+ numbers.txt
    73.27+218.38+14.15+9.18+16.60
    
    $ paste -sd+ numbers.txt | bc
    331.58
    • Σn where 1<=n<=100000

      $ seq 100000 | paste -sd+ | bc -l
      5000050000
  • $ paste -sd' ' numbers.txt | jq -s add
    331.58

number conversion

[!NOTE|label:references:]

  • tips

ibase and obase params order matters, but not always. Hex values must be in UPPERCASE.

  • the decimal can be ignored in ibase or obase, or we can say, the default ibase and obase are all 10.

CONVERT
IN BC
OUTPUT

八进制 → 二进制

echo "ibase=8; obase=2; 77" | bc

111111

十进制 → 十六进制

echo "obase=16; 255" | bc

FF

十六进制 → 十进制

echo "ibase=16; FF" | bc

255

二进制 → 十进制

echo "ibase=2; 101010" | bc

42

#!/usr/bin/env bash

function convertBase() {
  local from_base=''
  local to_base=''
  local value=''
  local upper=false

  while [[ $# -gt 0 ]]; do
    case "$1" in
      -f|--from  ) from_base="$2" ;  shift 2 ;;
      -t|--to    ) to_base="$2"   ;  shift 2 ;;
      -v|--value ) value="$2"     ;  shift 2 ;;
      -u|--upper ) upper=true     ;  shift   ;; # uppercase hex
      -h|--help  ) echo "Usage: convertBase -f <from_base> -t <to_base> -v <value> [-u]"
                   echo "Supported bases: bin, dec, oct, hex"
                   return 0
                   ;;
      *          ) echo "Unknown option: $1" >&2; return 1 ;;
    esac
  done

  # Map base names to numeric bases for bc
  declare -A base_map=(
    [bin]=2
    [oct]=8
    [dec]=10
    [hex]=16
  )

  local ibase="${base_map[$from_base]}"
  local obase="${base_map[$to_base]}"

  if [[ -z "$ibase" || -z "$obase" ]]; then
    echo "Error: Unsupported base. Use bin, dec, oct, hex." >&2
    return 1
  fi

  # For hex input, force uppercase since bc expects that
  if [[ "$from_base" == "hex" ]]; then
    value="${value^^}"
  fi

  # Use bc to convert
  result=$(echo "obase=$obase; ibase=$ibase; $value" | bc)

  # Format output (uppercase optional)
  if [[ "$upper" == true && "$to_base" == "hex" ]]; then
    result="${result^^}"
  fi

  echo "$result"
}
# usage
$ convertBase -f hex -t dec -v FF         # → 255
$ convertBase -f bin -t dec -v 1011       # → 11
$ convertBase -f dec -t hex -v 255        # → ff
$ convertBase -f dec -t hex -v 255 -u     # → FF
$ convertBase -f dec -t bin -v 42         # → 101010
$ convertBase -f oct -t hex -v 77         # → 3F
$ convertBase -f bin -t oct -v 110101     # → 65
  • supported base keywords

NAME
BASE
DESCRIPTION

bin

2

Binary

oct

8

Octal

dec

10

Decimal

hex

16

Hexadecimal

Common Bases

CONVERSION TYPE
TOOL / METHOD
EXAMPLE

Base → Decimal

$((base#value))

echo $((2#1010)) → 10

Decimal → Hex

printf

printf "%X\n" 255 → FF

Decimal → Binary

bc

bc < <(echo "obase=2; 42") → 101010

Octal → Decimal

$((8#17)) $((017))

echo $((8#17)) → 15

Hex → Decimal

$((16#FF)) $((0xFF))

echo $((16#FF)) → 255

  • base → decimal

BASE
TO
SYNTAX
INTERPRETED NUMBER
OUTPUT (DECIMAL)
DESCRIPTION

2

10

$((2#1011))

1011

11

BINARY

8

10

$((8#17))

17

15

OCTAL

8

10

$((017))

17

15

OCTAL

10

10

$((10#42))

42

42

DECIMAL

16

10

$((16#1A3F))

1A3F

6719

HEXADECIMAL

16

10

$((0x1A3F))

1A3F

6719

HEXADECIMAL

36

10

$((36#Z))

Z

35

ALPHANUMERIC MAX

64

10

$((64#_))

_

63

MAX BASE IN BASH

binary <> decimal <> hexadecimal

[!NOTE]

  • obase : [o]utput base

  • ibase : [i]utput base

# bin -> dec
$ bc <<< 'ibase=2;11111111;11111111;11000000;00000000' | paste -sd. -
255.255.192.0

# bin -> hex
$ bc <<< 'obase=16;ibase=2;11111111;11111111;11000000;00000000' | awk '{ printf "%04s\n", $1 }' | paste -sd. -
00FF.00FF.00C0.0000

# dec -> bin
$ bc <<< 'ibase=10;obase=2;255;255;240;0' | numfmt --format %08f | paste -sd' ' -
11111111 11111111 11110000 00000000

# dec -> hex
$ bc <<< 'ibase=10;obase=16;255;255;240;0' | awk '{ printf "%04s\n", $1 }' | paste -sd. -
00FF.00FF.00F0.0000

# hex -> bin
$ bc <<< 'ibase=16;obase=2;FF;FF;EE;0A' | numfmt --format %08f | paste -sd' ' -
11111111 11111111 11101110 00001010

# hex -> dec
$ bc <<< 'ibase=16;FF;FF;EE;0A' | paste -sd. -
255.255.238.10

to decimal

FROM
TO
COMMAND (BASH)
EXAMPLE
OUTPUT
COMMENT

Binary

Decimal

echo $((2#1010))

2#1010

10

-

Hex

Decimal

echo $((16#FF))

16#FF

255

-

Hex

Decimal

echo $((0xFF))

16#FF

255

0x - convert from hex

Octal

Decimal

echo $((8#77))

8#77

63

-

Octal

Decimal

echo $((077))

8#77

63

0 - convert from octal

  • from hexadecimal

    $ echo "ibase=16; F" | bc
    15
    
    # [o]utput base:       0xA -> 10
    #                       ^
    $ echo "ibase=16; obase=A; F" | bc
    15
    
    # or obase first
    $ echo "obase=10; ibase=16; F" | bc
    15
    
    # or
    $ echo $((0xF))
    15
  • from octal

    # obase (decimal) first
    $ echo "obase=10; ibase=8; 17" | bc
    15
    
    # or
    #                     ╭─ 012 -> 10
    #                     --
    $ echo "ibase=8;obase=12; 17" | bc
    15
    
    # or
    $ echo $((017))
    15

to hexadecimal

[!TIP]

  • if convert from decimal, the ibase can be ignored, or we can say, the default ibase is 10.

FROM
TO
COMMAND
EXAMPLE
OUTPUT

Binary

Hex

echo "obase=16; ibase=2; 101011" | bc

101011 → hex

2B

Octal

Hex

echo "obase=16; ibase=8; 77" | bc

77 (octal) → hex

3F

Decimal

Hex

echo "obase=16; 255" | bc

255

FF

Decimal

Hex

printf "%X\n" 255

255

FF

  • from decimal

    $ echo "obase=16; 15" | bc
    F
    
    # or
    $ echo "ibase=10;obase=16; 15" | bc
    F
  • from octal

    $ echo "obase=16; ibase=8; 17" | bc
    F
    
    # or
    $ printf "%x\n" 017
    f

to octal

FROM
TO
COMMAND
EXAMPLE
OUTPUT

Binary

Octal

echo "obase=8; ibase=2; 1010" | bc

1010 → base 2 → base 8

12

Decimal

Octal

printf "%o\n" 42

42

52

Hex

Octal

echo "obase=8; ibase=16; FF" | bc

FF → hex to octal

377

  • from hexadecimal

    $ echo "ibase=16;obase=8; F" | bc
    17
  • from decimal

    $ echo "obase=8; 15" | bc
    17
    
    # or
    $ echo "ibase=10;obase=8; 15" | bc
    17

to binary

[!NOTE|label:references:]

FROM
TO
COMMAND
EXAMPLE
OUTPUT

Octal

Binary

echo "obase=2; ibase=8; 77" | bc

8#77 → obase=2

111111

Decimal

Binary

echo "obase=2; 42" | bc`

42

101010

Hex

Binary

echo "obase=2; ibase=16; FF" | bc

ibase=16; FF → obase=2

11111111

  • from decimal

    $ bc <<< 'obase=2;15'
    1111
    
    $ bc -l <<< 'obase=2;0;0;15;255' | xargs
    0 0 1111 11111111
    
    $ bc -l <<< 'obase=2;0;0;15;255' | awk '{ printf "%08d\n", $0 }' | xargs
    00000000 00000000 00001111 11111111
    
    $ printf "%08d\n" $(echo "obase=2; 0;0;15;255" | bc) | xargs
    00000000 00000000 00001111 11111111
    
    $ bc <<< 'obase=2; 0;0;15;255' | numfmt --format=%08f | xargs
    00000000 00000000 00001111 11111111

to unicode ( hexadecimal )

[!NOTE|label:references:]

# 0x41 -> `A`; 0x61 -> `a`

# decimal -> hexadecimal
$ printf "$(printf %04x 65)\n"
0041

# \u<4-digits-hex>
$ printf "\u$(printf %04x 65)\n"
A

# \U<8-digits-hex>
$ printf "\U$(printf %08x 67147)\n"
𐙋

$ single_unicode_char="😈"
$ printf %d "'$single_unicode_char"
128520
$ printf "$(printf %08x 128520)\n"
0001f608
$ printf "\U$(printf %08x 128520)\n"
😈
  • more

    $ single_unicode_char="😈"
  • to hexadecimal

    #         ╭─ hexadecimal
    $ printf %x "'$single_unicode_char'"
    1f608
    $ printf %08X "'$single_unicode_char'"
    0001F608
    #                                                               ╭─ hexadecimal
    $ echo -n $single_unicode_char | iconv -t UTF-32LE | od -A n -t x4
     0001f608
    $ printf "\U0001f608"
    😈
    
    # or
    $ printf %#x "'$single_unicode_char"
    0x1f608
    $ printf %#X "'$single_unicode_char"
    0X1F608
    $ printf "\U$(printf %08x 0X1F608)\n"
    😈
    
    # or
    #                                           ╭─ hexadecimal
    $ echo -n $single_unicode_char | od -A n -t x1
     f0 9f 98 88
    $ printf %b "\xf0\x9f\x98\x88"
    😈
    #       ╭─ works without `%b` as well
    $ printf "\xf0\x9f\x98\x88"
    😈
  • to octal

    #         ╭─ octal
    $ printf %o "'$single_unicode_char'"
    373010
    #                                                               ╭─ octal
    $ echo -n $single_unicode_char | iconv -t UTF-32LE | od -A n -t o4
     00000373010
    $ printf "\U$(printf %08x "00000373010")"
    😈
    
    # or
    #       ╭─ without `\n`
    $ echo -n $single_unicode_char | od -A n -t o1
     360 237 230 210
    $ printf %b "\360\237\230\210"
    😈

number converting from file

  • octal

    $ cat -p octal-data-file.txt
    7
    10
    11
    12
    13
    14
    15
    16
    17
    20
    21
    
    # octal -> hexadecimal
    $ ( echo "obase=16; ibase=8" ; cat octal-data-file.txt ) | bc
    7
    8
    9
    A
    B
    C
    D
    E
    F
    10
    11
    
    # octal -> decimal
    $ ( echo "obase=10; ibase=8" ; cat octal-data-file.txt ) | bc
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
  • hexadecimal

    $ cat -pp hex-data-file.txt
    9
    A
    B
    C
    D
    E
    F
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    1A
    1B
    1C
    1D
    1E
    1F
    20
    
    # hexadecimal -> octal
    $ ( echo "obase=8; ibase=16" ; cat hex-data-file.txt ) | bc
    11
    12
    13
    14
    15
    16
    17
    20
    21
    22
    23
    24
    25
    26
    27
    30
    31
    32
    33
    34
    35
    36
    37
    40
    
    # hexadecimal -> decimal
    $ ( echo "obase=10; ibase=16" ; cat hex-data-file.txt ) | bc
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32

advanced computing

[!NOTE|label:references:]

logarithm

  • bc

    $ bc -l <<< 'l(9)/l(3)'
    2.00000000000000000000
    • $ bc -l <<< 'l(512)/l(2)'
      9.00000000000000000008
  • awk

    $ echo 512 | awk '{print log($1)/log(2)}'
    9

power

  • bc

    $ bc <<< '2^3'
    8
  • $(())

    $ echo $(( 2**8 ))
    256

square

$ bc -l <<< 'sqrt(2)'
1.41421356237309504880

$ bc <<< 'sqrt(2)'
1

Bash 的算术运算
Linux 中bc命令实现自然对数、指数运算、自然指数、平方根的运算
bc to Perform Advanced Arithmetic Operations in BASH
Using vim as calculator
Shell command to sum integers, one per line?
Summing a List of Numbers
Shell command to sum integers, one per line?
paste + bc
xargs + bc
sed + bc
jq
$(())
How can I quickly sum all numbers in a file?
awk
paste && bc
jq
Understand "ibase" and "obase" in case of conversions with bc?
Linux / UNIX: bc Convert Octal To Hexadecimal or Vise Versa
Prevent bc from auto truncating leading zeros when converting from hex to binary
How to make bc output a desired number of base-2 binary digits
Convert a character from and to its decimal, binary, octal, or hexadecimal representations in BASH / Shell
How to calculate the log of a number using bc?
指数与对数
n次方根
🫠
$(())
sum
awk
datamash
bc
jq
sum from file
number conversion
Common Bases
binary <> decimal <> hexadecimal
to decimal
to hexadecimal
to octal
to binary
to unicode ( hexadecimal )
number converting from file
advanced computing
logarithm
power
square