Bash command line tips
1. Brace expansion:
$ echo {a..c}{0..5}
a0 a1 a2 a3 a4 a5 b0 b1 b2 b3 b4 b5 c0 c1 c2 c3 c4 c5
eg. use to remove files with common prefix:
$ rm program.{c,h,o}
2. Base Conversion:
$ echo $((0x1FFFFF))
2097151
$ echo $[0x1fffff]
2097151
$ echo $[16#1fffff]
2097151
$ echo $[2#111111]
63
$ declare -i result
$ result=2#111111
$ echo $result
63
For arbitrary conversions to bases other than decimal use dc and here strings <<<:
$ dc <<< "2o 10i 17 p"
10001
$ dc <<< "2o 16i 10F p"
100001111
(2o = output base 2, 16i = input base 16, p = print)
3. Arithmetic:
$ echo $[5*33]
165
$ echo $[2**31]
2147483648
$ echo $[2**52%53]
1
For large numbers and floating point, use bc and here strings <<<:
$ echo $[2**64]
0
$ bc <<< "2^64"
18446744073709551616
$ bc <<< "2.3+5.8"
8.1
$ bc <<< "2.3222+5.8111101"
8.1333101
add -l to specify math functions, eg a()=atan():
$ bc -l <<< "4*a(1)"
3.14159265358979323844
$ bc -l <<< "scale=100;4*a(1)"
3.14159265358979323846264338327950288419 7169399375105820974944592307\
8164062862089986280348253421170676
4. for loop C-style:
$ for ((i=o; i<10; i++)); do echo -n $i" "; done
0 1 2 3 4 5 6 7 8 9
nth roots of 2, n=1-10, to 20 decimal places:
$ for ((n=1;n<=10;n++)); do bc -l <<< "scale=20;e(l(2)/$n)"; done
1.99999999999999999998
1.41421356237309504878
1.25992104989487316476
1.18920711500272106671
1.14869835499703500679
1.12246204830937298142
1.10408951367381233764
1.09050773266525765919
1.08005973889230616986
1.07177346253629316421
5. while read loop:
<command> | while read i; do echo "$i"; done
or, if newlines are not the delimiter specify IFS:
IFS=:;<command> | while read i; do echo "$i"; done
6. pseudo random number:
$ echo $RANDOM
29833
$ echo $RANDOM
1123
$ echo $RANDOM
4235
7. string parsing, eg extract filename prefix:
$ str="file.txt"
$ echo ${str%%.*}
file
$ echo {a..c}{0..5}
a0 a1 a2 a3 a4 a5 b0 b1 b2 b3 b4 b5 c0 c1 c2 c3 c4 c5
eg. use to remove files with common prefix:
$ rm program.{c,h,o}
2. Base Conversion:
$ echo $((0x1FFFFF))
2097151
$ echo $[0x1fffff]
2097151
$ echo $[16#1fffff]
2097151
$ echo $[2#111111]
63
$ declare -i result
$ result=2#111111
$ echo $result
63
For arbitrary conversions to bases other than decimal use dc and here strings <<<:
$ dc <<< "2o 10i 17 p"
10001
$ dc <<< "2o 16i 10F p"
100001111
(2o = output base 2, 16i = input base 16, p = print)
3. Arithmetic:
$ echo $[5*33]
165
$ echo $[2**31]
2147483648
$ echo $[2**52%53]
1
For large numbers and floating point, use bc and here strings <<<:
$ echo $[2**64]
0
$ bc <<< "2^64"
18446744073709551616
$ bc <<< "2.3+5.8"
8.1
$ bc <<< "2.3222+5.8111101"
8.1333101
add -l to specify math functions, eg a()=atan():
$ bc -l <<< "4*a(1)"
3.14159265358979323844
$ bc -l <<< "scale=100;4*a(1)"
3.14159265358979323846264338327950288419
8164062862089986280348253421170676
4. for loop C-style:
$ for ((i=o; i<10; i++)); do echo -n $i" "; done
0 1 2 3 4 5 6 7 8 9
nth roots of 2, n=1-10, to 20 decimal places:
$ for ((n=1;n<=10;n++)); do bc -l <<< "scale=20;e(l(2)/$n)"; done
1.99999999999999999998
1.41421356237309504878
1.25992104989487316476
1.18920711500272106671
1.14869835499703500679
1.12246204830937298142
1.10408951367381233764
1.09050773266525765919
1.08005973889230616986
1.07177346253629316421
5. while read loop:
<command> | while read i; do echo "$i"; done
or, if newlines are not the delimiter specify IFS:
IFS=:;<command> | while read i; do echo "$i"; done
6. pseudo random number:
$ echo $RANDOM
29833
$ echo $RANDOM
1123
$ echo $RANDOM
4235
7. string parsing, eg extract filename prefix:
$ str="file.txt"
$ echo ${str%%.*}
file