$ seq 10 | awk '{s+=$1} END {print s}'
55
# or
$ awk 'BEGIN{print '"1+2+3"'}'
6
$ 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
$ seq 10 | datamash sum 1
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 ))
[!NOTE|label:sample file:]
$ cat numbers.txt
73.27
218.38
14.15
9.18
16.60
[!NOTE|label:references:]
ibase
and obase
params order matters, but not always. Hex values must be in UPPERCASE.
binary <> decimal <> hexadecimal
# 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 unicode ( hexadecimal )
# 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"
😈
number converting from file
$ bc -l <<< 'sqrt(2)'
1.41421356237309504880
$ bc <<< 'sqrt(2)'
1