Operators
Arithmetic
Bash does not know how to add, reduce, multiply... 2 solutions:
Create variables:
var=$((number operation number)); echo $vartest=$((50*50)); echo $test
Command 'bc':
apt install bcecho "number operation number" | bcecho "50*50" | bc
Comparison
0 = TRUE
1 = FALSE
Numbers:
-eq is equal
-ne is not equal
Numbers:
-gt is greater
-ge is greater or equal
Numbers:
-lt is lower
-le is lower or equal
Text:
= is equal
!= is not equal
Text:
\> is greater
Text:
\< is lower
Commands to use:
test,[,[[var1=100 var2=101 test $var1 -eq $var2//[ $var1 -eq $var2] echo $?result will be 1 -> falsevar1="hello" var2="goodbye" test "$var1" != "$var2" echo $?result will be 0 -> true
Find substring
var1="ImaWildSubstring, can you find me?" [[ "$var1" == *"WildSubstring"* ]] echo $?result will be 0 -> true
Logic
&& and
|| or
! false
v1=20 v2=30 v3=40 v4=50 test $v1 -ne $v2 && test $v3 -gt $v4 echo $?result will be 0 -> true
File test existence
Commands to use: test, [, [[
-echecks if file exists.-fchecks if file exists and it is regular.Regular would be files themselves, not directories.
-dchecks if directory exists.-r -w -xchecks if file has reading, writing and executing permissions
Last updated