Bash Shell Scripting Language - Documentation and Code Snippets

ยท 771 words ยท 4 minute read

This is a bash shell scripting which can be found on Unix, Linux and Mac. You can install bash on the Linux subsystem on Windows too.

The first line is #! /bin/bash because the bash program is in /bin/bash, you can know the path where the bash is by this command which bash.

You can use echo Hello, World! or echo "Hello, World".

Variables & Print out them ๐Ÿ”—

NAME = "Abanoub"
echo "My name is $NAME"

or you can use this

NAME = "Abanoub"
echo "My name is ${NAME}"

Get Data From User ๐Ÿ”—

read -p "Enter your name: " Name
echo "Hello, $NAME!"

If Statement ๐Ÿ”—

The simple if statement syntax of bash script:

if ["$NAME" == "Abanoub"]
echo "Your name is Abanoub!"

NOTE THAT : the end of if is fi (the reverse letters of if).

The if else statement syntax in bash script:

if ["$NAME" == "Abanoub"]
echo "Your name is Abanoub!"
echo "Your name is NOT Abanoub"

The else if(elif) condition statement syntax in bash scripting:

if ["$NAME" == "Abanoub"]
echo "Your name is Abanoub!"
elif ["$NAME" == "JACK"]
echo "Your name is JACK!"
echo "Your name is NOT Abanoub NOR Jack!"

Logic Comparisons ๐Ÿ”—

You can use those operators:

Logic OperatorMeaning
-eqequal to (the same meaning of == in other languages)
-nenot equal (the same meaning of != in other languages)
-gtgreater than (the same meaning of > in other language)
-gegreater than or equal to (the same meaning of >= in other languages)
-ltless than (the same meaning of < in other languages)
-leless than or equal to (the same meaning of <= in other languages)

and use them like this:

NUM1 = 3
NUM2 = 5
if ["$NUM1" -gt "$NUM2"]
echo "$NUM1 is greater than $NUM2"

File Conditions ๐Ÿ”—

These are the file condition flags:

-dis directory?
-eexists? (usually we use -f instead)
-fa file?
-gis group id set?
-snon-zero size?
-uuser id is set?

and use them like this:

FILE = "test.txt"
if [ -f "$FILE" ]
echo "$FILE is a file"
echo "$FILE is NOT a file"

Case Statement ๐Ÿ”—

Case is called switch case in other languages, and some modern languages call it when case such as Kotlin programming language.

Here is the case statement in bash scripting:

read -p "Are you 25? Y/N" ANSWER
case "$ANSWER" in
    echo "Your age is mine :)"
    echo "Nooo, your age is different than mine :("
    echo "Please enter y/yes or n/no"

Note that:

[nN] is a way of giving two probabilities small n or capital N.

[yY][eE][sS] is the word yes or YES or any combination of small and capital letters to compose a YeS word.

*) this is the default option in the case statement which is called default in other programming/scripting languages.

esac is the closing of the case statement as it is reversed letters of case . This is the way of ending statements in bash script.

For Loop ๐Ÿ”—

NAMES = "Abanoub Jack John Smith"
for NAME in $NAMES
echo "Hello, $NAME"

Here is a script to rename all text files *.txt at once by a script:

FILES = $(ls *.txt)
NEW = "new"
for FILE in $FILES
echo "Renaming $FILE to new-$FILE"

While Loop ๐Ÿ”—

Here is a while loop to read nfile.txt line by line.

LINE = 1
while read -r CURRENT_LINE
done < "./nfile.txt"

Bash Script Function Syntax ๐Ÿ”—

function sayHello(){
echo "Hello, World!"

We created a function to print out Hello, World! and call it to occur!

Bash Script Functions with Parameters ๐Ÿ”—

Here is how to write a function with params in bash script.

function greet(){
 echo "Hello, I am $1 and I am $2"
greet "Abanoub" "25"

The $1 is the first parameter, and $2 is the second parameter. So when we call the function, we should specify the two parameters in the same order like this greet "Abanoub" "25". This means that "Abanoub" is the first param $1 and "25" is the second param $2.

Final Tips ๐Ÿ”—

Now, you learned the syntax of bash scripting, but you need to learn the bash commands. You will use the bash commands in the bash scripting syntax to build the script you want to run automatically (terminal app).

waffarx cash back