Bash
From Ahmed Farag
To write bash code start with #!/bin/bash → because system show this file as ASCII text
echo or printf we use it for printing
BASH → use interpreter bash (print correct line then print the error is is found)
implementation
to implement variable
#!/bin/bash
# no spaces before and after equal -> before space execute as a command
name="irix"
echo $name # to print the value for variable
printf $namethe difference between echo,printf :
printf → print text in the same line
echo → print text in new line (to print text in the same line we use option -n )
To save the value for execution command we use -> `var=`ls
mathematical operation
$(())
num1=5;num2=4 echo $(($num1+$num2)) # sum num1,num2 the print the value for summationwith all operation except postfix and prefix ((x++))
let
let x=1+10 echo $x # output is 11expr
# take care with space in expr x= expr 1 + 10 # same spaces echo $x
input
to take input from user we use
# first way
echo "enter your name"
read name # read use to read input from user and stored it in variable
# second way
num1=$1 # input in var1
num2=$2 # input in var2
# ./calc.sh 10 5 read → we have more option for it like :
-s —> make input non visible (like password in linux)
read -s $name-p —> print text (like input in python)
read -p "what is your name: " $namearray
we use
setto implement arrayset 10 20 30 50 90 echo $(($1+$5)) # the value is 100 set what is your name echo $1 # the output is whatimplement array , we use
${}to print arrayos=('linux' 'mac' 'windows' 'android') # WE USE () TO implemant array echo ${os[0]} # output linux # to print all element in the array we use * echo ${os[*]} #remove element from array we use unset os[1] #insert value os[2]='hello'connection
no
concatenationin bashset 10 20 30 50 90 echo $1 $2 $3 # output is 10 20 30
execution
to execute command on system $()
var=$(whoami)
# or
var=`whoami`
echo $var
echo $(whoami)
#or
echo $WHOAMIcondition
if condition
if [ $num1 == $num2 ] # must space between [] then echo "yes" elif [ $num1>$num2 ] then echo "keep" else echo "no" fi # to end conditionThe -f checked if the file existed.
The -w checked if the file was writable, without write permissions we wouldn't be able to output our text into the file.

image.png Test condition
test 001==1 && echo "True" || echo "False" # output is false (because it compare two string not two integer) To compare integer use -eq,-ge,etc..just check on two variable like
?in cpp
loop
echo {1..6} → regular expiration (out put from 1 to 6)
For loop
for $variable_name in 1 2 3 4
do
expiration
done
seqq="hello world" for i in $seqq do echo -n $i " " done #output -> hello worldWhile loop
while [condition]
do
expiration
done
$i=0 while [true] do echo $i i=$((i+1)) done
To Debug your code we use :
set -x
code here
set +x
# show all operation step by stepString
compare two strings
if [ $str1=$str2 ]
then
echo "print"
elif [ $str1\\<$str2 ] # greeter than \\< , less than >\\To count chars in word
x="hello world"
length=`echo $x | wc -c` # -c is option refer to chars
echo $lengthSplit → IFS
x="what is the name of the index file"
IFS=" " # setting space as a delimiter
read -ra arr <<< "$x" # reading str as an array as token sepeated by IFS
echo ${arr[0]} # output -> whatSubstring ${var:start:end}
x="what is the name of the index file"
substr=${x:0:10}
echo $substrFunction
function_name(){commands;}
or
function_name(){
commands
}
name(){
echo hello world
}
name # call functionNotes
exit 0 → to exit from code
Last updated
