This document describes basic shell scripting using Termux on an Android device, specifically a Redmi Pad 11 with a Bluetooth keyboard and mouse. The scripts are edited using nano in Termux and Quickedit on the tablet.
$ uname -a
Linux localhost 5.15.148-android13-8-00004-ge488687c12ef-ab11838684 #1
SMP PREEMPT Tue May 14 09:27:18 UTC 2024 aarch64 Android
$ pwd > /data/data/com.termux/files/home
$ whoami > u0_a261
$ cd /TBCM111
$ nano b001.sh # Edit shell script
$ chmod +x b001.sh
$ ./b001.sh
#!/bin/bash
echo "#b001---add integer-------------"
x=10
y=100
echo "{\"x\":$x}"
echo "{\"y\":$y}"
echo "{\"x+y\": $((x+y))}"
echo "#b002---add character-------------"
x="10"
y="100"
echo "{\"x\":$x}"
echo "{\"y\":$y}"
echo "{\"x+y\": $x+$y}"
echo "{\"xy\": $x$y}"
echo "#b003---add float---------------"
x="10"
y="100"
z="10.5" # no support float directly
echo "{\"x\":$x}"
echo "{\"y\":$y}"
echo "{\"z\":$z}"
sum=$(echo "$x + $y + $z" | bc)
echo "{\"sum\":$sum}"
echo "#b004--expr: non-integer argument--"
x="10"
y="100"
z="10.5"
sum=$(expr $x + $y + $z)
echo "{\"sum\":$sum}"
#b001---add integer-------------
{"x":10}
{"y":100}
{"x+y": 110}
#b002---add character-------------
{"x":10}
{"y":100}
{"x+y": 10+100}
{"xy": 10100}
#b003---add float---------------
{"x":10}
{"y":100}
{"z":10.5}
{"sum":120.5}
#b004--expr: non-integer argument--
expr: non-integer argument
{"sum":}
$(( )).bc, as bash does not natively support floats.expr with non-integer arguments.Key Takeaways:
$(( )) for integer arithmetic.bc for floating-point arithmetic.expr only works with integers.