Try   HackMD

Bash Script Beginner Note

Command

Check your terminal have bash or not

bash version

Show file content

cat <file>

Show file content reversely

tac <file>

Show file content from head

head <file>
head -n <number_of_lines> <file>
head -c <number_of_bytes> <file>

Show file content from tail

tail <file>
tail -n <number_of_lines> <file>
tail -f <file>

Search bash command absolute path

which bash

Change current directory to specific directory

cd <directory>
ls
ls -al

Add a new file in current directory

touch <file>

Open file with vim editor

vim <file>
chmod +x <file or directory>
chmod 755 <file or directory>

Output text

echo "text"
echo "text" > <file>
echo $SHELL

Script

Thanks to @linuxhint for youtube bash script tutorial.
Here are some basic bash script example to get concept from smooth way.

1. Hello Bash Script

Output text

#! /bin/bash

echo "Hello Bash Script"

Store text in file

#! /bin/bash

echo "Hello Bash Script" > file.txt

2. Redirect to file

Replace file context with input text

#! /bin/bash

cat > file.txt

Append input text to file context

#! /bin/bash

cat >> file.txt

3. Comment

Single line comment

#! /bin/bash

# Comment

Multiple line comment

#! /bin/bash

: '
Comment1
Comment2'

4. Conditional Statements

Abbr Meaning
-eq equal
-ne not equal
-gt greater than
-ge greater than or equal to
-lt less than
-le less than or equal to
-a and
-o or

Equal case -eq

#! /bin/bash

count=10
if [ $count -eq 10 ]
then
    echo "Count is 10"
else
    echo "Count is otherwise"
fi

Compare case >, <

#! /bin/bash

count=10
if (( $count > 10 ))
then
    echo "Count is greater than 10"
elif (( $count >= 7 ))
then
    echo "Count is greater than or equal to 7"
else
    echo "Count is otherwise"
fi

And case &&, -a

#! /bin/bash

count=10
if [ "$count" -gt 10 ] && [ "$count" -lt 40 ]
# [[ "$count" -gt 10 && "$count" -lt 40 ]]
# [ "$count" -gt 10 -a "$count" -lt 40 ]
then
    echo "Count is correct"
else
    echo "Count is not correct"
fi

Or case ||, -o

#! /bin/bash

count=10
if [ "$count" -gt 10 ] || [ "$count" -lt 40 ]
# [[ "$count" -gt 10 || "$count" -lt 40 ]]
# [ "$count" -gt 10 -o "$count" -lt 40 ]
then
    echo "Count is correct"
else
    echo "Count is not correct"
fi

5. Loops

While loop

#! /bin/bash

count=1
while [ $count -lt 10 ]
do
    echo "$count"
    count=$(( count+1 ))
done

Until loop

#! /bin/bash

count=1
until [ $count -ge 10 ]
do
    echo "$count"
    count=$(( count+1 ))
done

For loop

#! /bin/bash

for i in 0 1 2 3 4 5
# for i in {0..5}
# for i in {0..5..1}
# for (( i=0; i<=5; i++ ))
do
    echo $i
done

Break

#! /bin/bash

for (( i=0; i<=5; i++ ))
do
    if [ $i -eq 3 ]
    then
        break
    fi
    echo $i
done

Continue

#! /bin/bash

for (( i=0; i<=5; i++ ))
do
    if [ $i -eq 3 ]
    then
        continue
    fi
    echo $i
done

6. Script Input

$0 shows the file path related to current folder

#! /bin/bash

echo $0 $1 $2 $3

Execute in terminal

helloScript.sh a b c

Result

helloScript.sh a b c

args are dynamic depends on what user inputs

#! /bin/bash

args=("$@")

# echo ${args[0]} ${args[1]} ${args[2]}
echo $@ # args value
echo $# # args length

Execute in terminal

helloScript.sh a b c

Result

a b c
3
#! /bin/bash

while read line
do
    echo "$line"
done < "${1:-/dev/stdin}"

Execute in terminal

helloScript.sh <file>

Result

# Echo file content line by line

7. Script Output

8. How to Send Output From One Script to Another Scrpt

9. String Processing

10. Numbers and Arithmetic

11. Declare Command

12. Array

13. Functions

14. Files and Directories

15. Send Email Via Script

16. Curl in Scripts

17. Professional Menus

18. Wait for filesystem events with inotify

19. Introduction to grep

20. Introduction to awk

21. Introduction to sed

Reference

tags: Work Bash Script