lets bash scripting
Estimated reading time: 6 minutesbash
bash
= Bourne-Again SHell
comments
# this is single line comment
echo "kamal is a code artist" # this is comment
# output: kamal is a code artist
variable
local variable
name="kamal"
echo $name
# output: kamal
local keyword uses inside function
name="kamal"
user(){
local name="jamal"
echo $name
}
echo $name
#output:
# jamal
# kamal
global variable
export price=51
default varibale
name="jamal"
echo ${name:="kamal"}
# jamal
echo ${name:="kamal"}
# kamal
read
read -p "submit your name: " name
echo $name
string
status="Mostafa Kamal is a code artist"
echo $status
# Mostafa Kamal is a code artist
echo "$status"
# Mostafa Kamal is a code artist
name="kamal"
echo 'this is $name'
# thi is $name
echo "this is $name"
# this is kamal
arithmatic
$(( .. ))
x=5
y=7
echo (( x + y ))
# 12
array
users=("kamal" "jamal" "maruf" "sadia")
echo ${users[1]}
# output: jamal
users=("kamal" "jamal" "maruf" "sadia")
echo ${users[@]} # [@] or [*] retrive all
# output: kamal jamal maruf sadia
users=("kamal" "jamal" "maruf" "sadia")
echo ${#users[@]}
# output : 4
# retrive array size
array add
users=("kamal" "jamal" "maruf" "sadia")
employee=("kamrul" "${users[@]}" "faruk")
echo ${employee[@]}
# output: kamrul kamal jamal maruf sadia faruk
array slice
users=("kamal" "jamal" "maruf" "sadia")
echo ${users[@]:1:2}
# output: jamal maruf
delete array ele
users=("kamal" "jamal" "maruf" "sadia")
unset users[1]
echo ${users[@]}
# output: kamal maruf sadia
operator
arithmatic operator | description |
---|---|
-eq | equal |
-ne | not equal |
-lt | less than |
-le | less than or equal |
-gt | greater than |
-ge | greater than or equal |
condition
- single square bracket
[condition]
or double square bracket[[condtion]]
- ubuntu system basicaly not support
[[ not support ]]
- access
[[ double]]
commandsudo bash myfile.sh
- On Ubuntu systems,
/bin/sh
isdash
, notbash
, anddash
doesnot support
thedouble bracket
- access
if
if [ 2 -eq 2 ]; then
echo "the number is 2"
fi
# output : the number is 2
if-else
number=2
if [ $number -eq 3 ]; then
echo "the number is 2"
else
echo "the number is not 2"
fi
# output : the number is 2
elseif
number=2
if [ $number -eq 3 ]; then
echo "the number is 3"
elif [ $number -eq 2 ]; then
echo "the number is 2"
else
echo "the number is not 2"
fi
# output : the number is 2
case
number=5
case $number in
2 )
echo "the number is 2"
;;
3 )
echo "the number is 3"
;;
* )
echo "not 2 or 3"
;;
esac
# output: not 2 or 3
loop
for
users=("kamal" "jamal" "sadia" "maruf")
for i in ${users[@]}; do
echo $i
done
# kamal jamal sadia maruf
for (( i=0; i < 10 ; i++)); do
echo $i
done
# 0 1 2 3 4 5 6 7 8 9
continue
for (( i=0; i < 10; i++)); do
if [ $(( i % 2 )) -eq 0]; then
continue
fi
echo $i
done
# 1 3 5 7 9
break
for (( i=0; i < 10; i++)); do
if [ $i -eq 5]; then
break
fi
echo $i
done
# 0 1 2 3 4
while
i=0
while [ $i -lt 10 ]; do
echo $i
i=$(( i + 1 ))
done
# 0 1 2 3 4 5 6 7 8 9
until
until
is opposite ofwhile
i=0
until [ $i -gt 10 ]; do
echo $i
i=$(( i + 1 ))
done
# 0 1 2 3 4 5 6 7 8 9
command
multiple command
uname
id -u
whoami
and comannd
# uname && id -u
or comannd
# unamed || id -u
# unamed is wrong command
# uname is actual command
function
read name
create(){
adduser $1
}
create $name