Termux Script Syntax

Variable

name="Lanlanwi"
echo "$name"

if

x=1

if [ "$x" -eq 1 ]; then
  echo "It is 1"
fi
x=2

if [ "$x" -eq 1 ]; then
  echo "It is 1"
elif [ "$x" -eq 2 ]; then
  echo "It is 2"
else
  echo "Something else"
fi
if [ -f test.txt ]; then

for

for i in 1 2 3 do
  echo "$i"
done
for file in *.txt do
  echo "$file"
done
for i in {1..30} do
  echo "$i"
done
for ((i=0; i<5; i++)) do
  echo "$i"
done
for i in {1..30} do
  if [ "$i" -eq 5 ]; then
    break
  fi
  echo "$i"
done
for i in {1..10} do
  if [ "$i" -eq 5 ]; then
    continue
  fi
  echo "$i"
done

while

i=5

while [ "$i" -gt 0 ] do
  echo "$i"
  i=$((i-1))
done
while [ -f test.txt ] do
  echo "File exists"
  sleep 2
done

case

read -p "Enter something: " input

case "$input" in
  hello)
    echo "Hello"
    ;;
  bye)
    echo "Goodbye"
    ;;
  *)
    echo "I don't understand"
    ;;
esac
case "$file" in
  *.txt)
    echo "Text file"
    ;;
  *.jpg)
    echo "Image file"
    ;;
esac

Argument

./script.sh hello 123
echo $1
echo $2
$0
The name of the script (or current shell)
$@
All arguments
$#
Number of arguments
shift
Shifts arguments to the left
($1 is removed, $2 becomes $1, etc.)

function

myfunc() {
  echo "Hello"
  echo "Hello"
}

myfunc
greet() {
  echo "Hello $1"
  echo "Hi $2"
}

greet Lanlanwi Sheban

Comment

# Comment