#!/bin/bash
a=/home/web
echo "My home folder is $a"
echo "My data folder is ${a}/data"
#!/bin/bash
# declare a array variable color
color=(red blue green)
# print all elements of the array
echo ${color[*]}
# set 4th element of the array
color[3]=yellow
echo ${color[*]}
# print third element of the array
echo "The sunflower is ${color[2]}"
# print the number of elements in array
echo ${#color[*]}
Variable names are exapnded within double quotes, but not within single quotes.
#!/bin/bash
string="abcdefg"
echo "$string"
echo "\$string"
echo '$string'
echo $'\U1f60e'
Repetition
for((i=1; i<=5; ++i)); do
echo ${i}
done
#!/bin/bash
function greeter() { echo "Hello ${USER}!"; }
greeter
#!/bin/bash
function greeter() { echo "Hello $1!"; }
greeter "My Friend"
#!/bin/bash
function f() {
if [ $1 -lt 0 ]; then
echo "negative"
elif [ $1 -le 9 ]; then
echo "one digit"
elif [ $1 -le 99 ]; then
echo "two digits"
else
echo "large!"
fi
}
f 21345890
Repetition
#!/bin/bash
count=10
i=0
while [ $i -lt $count ]; do
echo "$i"
i=$(($i+1))
done
It will be available on Github. Refer for any doubts!