Bash Shell Basics
Let's learn about the fundamentals of Shell scripts.

The default shell in the WebIDE is Zsh, we'll be writing Bash scripts in this lab. Bash (Bourne Again SHell) is the most common and widely used shell in Unix-like systems. Zsh (Z Shell) is an extended version of Bash with some improvements and features.
In Shell, if we don’t see any output that generally means the command is successful.
#!/bin/bash : This is call ‘shebang’ line. This is to tell the system to interprete as bash shell script.
Below is the 1st Bash Shell program.
#This is for cheating the hello.sh file labex:project/ $ touch hello.sh ##Inside the hello.sh file we write the below shell script which is our 1st shell script #!/bin/bash echo 'Hello, World!' ## Below is the command to execute it. #This is to give execute permission for that file via chmod and x is execute labex:project/ $ chmod +x hello.sh ##Below is the way to run the hello.sh file labex:project/ $ ./hello.sh Hello, World! #This is the output ## cat is for see the content of the file labex:project/ $ cat hello.sh #!/bin/bash echo 'Hello, World!'
##Writing variables in shell
labex:project/ $ touch /home/labex/project/variables.sh
labex:project/ $ chmod +x /home/labex/project/variables.sh
##Inside variable.sh
#!/bin/bash
PRICE_PER_APPLE=5
MyFirstLetters=ABC
greeting='Hello world!'
echo "Price per apple: $PRICE_PER_APPLE"
echo "My first letters: $MyFirstLetters"
echo "Greeting: $greeting"
##Let's execute it.
labex:project/ $ ./variables.sh
Price per apple: 5
My first letters: ABC
Greeting: Hello world!
##Use escaping charachter and preserving white spaces
#!/bin/bash
PRICE_PER_APPLE=5
MyFirstLetters=ABC
greeting='Hello world!'
# Escaping special characters
echo "The price of an Apple today is: \$HK $PRICE_PER_APPLE"
# Avoiding ambiguity
echo "The first 10 letters in the alphabet are: ${MyFirstLetters}DEFGHIJ"
# Preserving whitespace
echo $greeting
echo "$greeting"
##Output
labex:project/ $ ./variables.sh
The price of an Apple today is: $HK 5
The first 10 letters in the alphabet are: ABCDEFGHIJ
Hello world!
Hello world!
Command Substitution: See the below examples to understand.
# Command substitution
CURRENT_DATE=$(date +"%Y-%m-%d")
echo "Today's date is: $CURRENT_DATE"
FILES_IN_DIR=$(ls)
echo "Files in the current directory:"
echo "$FILES_IN_DIR"
UPTIME=$(uptime -p)
echo "System uptime: $UPTIME"
##Below are the output of the above
Today's date is: 2025-10-22
Files in the current directory:
birthdate.sh
variables.sh
System uptime: up 33 weeks, 16 hours, 40 minutes
$(date +"%Y-%m-%d")runs thedatecommand and captures its output.$(ls)runs thelscommand and captures its output.$(uptime -p)runs theuptimecommand with the-poption and captures its output.
labex:project/ $ touch /home/labex/project/arithmetic.sh
labex:project/ $ chmod +x /home/labex/project/arithmetic.sh
##Inside arithmatic.sh
#!/bin/bash
X=10
Y=5
# Addition
SUM=$((X + Y))
echo "Sum of $X and $Y is: $SUM"
# Subtraction
DIFF=$((X - Y))
echo "Difference between $X and $Y is: $DIFF"
# Multiplication
PRODUCT=$((X * Y))
echo "Product of $X and $Y is: $PRODUCT"
# Division
QUOTIENT=$((X / Y))
echo "Quotient of $X divided by $Y is: $QUOTIENT"
# Modulus (remainder)
REMAINDER=$((X % Y))
echo "Remainder of $X divided by $Y is: $REMAINDER"
# Increment
X=$((X + 1))
echo "After incrementing, X is now: $X"
# Decrement
Y=$((Y - 1))
echo "After decrementing, Y is now: $Y"
##Below is the output
labex:project/ $ ./arithmetic.sh
Sum of 10 and 5 is: 15
Difference between 10 and 5 is: 5
Product of 10 and 5 is: 50
Quotient of 10 divided by 5 is: 2
Remainder of 10 divided by 5 is: 0
After incrementing, X is now: 11
After decrementing, Y is now: 4
Using Environment variables such as $HOME, $LOGNAME, $SHELL, $PATH etc.
Note: export is used for creating new enviroment variable and unset is used for removing the environment variable.
labex:project/ $ touch /home/labex/project/environment.sh
labex:project/ $ chmod +x /home/labex/project/environment.sh
##Inside environment.sh
#!/bin/bash
# Displaying some common environment variables
echo "Home directory: $HOME"
echo "Current user: $LOGNAME"
echo "Shell being used: $SHELL"
echo "Current PATH: $PATH"
# Creating a new environment variable
export MY_VARIABLE="Hello from my variable"
# Displaying the new variable
echo "My new variable: $MY_VARIABLE"
# Creating a child process to demonstrate variable scope
bash -c 'echo "MY_VARIABLE in child process: $MY_VARIABLE"'
# Removing the environment variable
unset MY_VARIABLE
# Verifying the variable is unset
echo "MY_VARIABLE after unsetting: $MY_VARIABLE"
##below is the output
labex:project/ $ ./environment.sh
Home directory: /home/labex
Current user: labex
Shell being used: /usr/bin/zsh
Current PATH: /usr/lib/jvm/java-11-openjdk-amd64/bin:/usr/lib/jvm/java-11-openjdk-amd64/bin:/usr/lib/jvm/java-11-openjdk-amd64/bin:/usr/lib/jvm/java-11-openjdk-amd64/bin:/usr/lib/code-server/lib/vscode/bin/remote-cli:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/home/labex/.local/bin:/usr/local/go/bin:/home/labex/golang/bin:/usr/share/maven/bin:/usr/local/bin:/usr/sbin/nodejs/bin:/usr/sbin/yarn/bin:/home/labex/.local/bin:/usr/local/go/bin:/home/labex/golang/bin:/usr/share/maven/bin:/usr/local/bin:/usr/sbin/nodejs/bin:/usr/sbin/yarn/bin
My new variable: Hello from my variable
MY_VARIABLE in child process: Hello from my variable
MY_VARIABLE after unsetting:
Passing argument in bash Shell
#Inside arguments.sh
## In the below script $0, $1, $2 and $3 are arguments or parameters
#!/bin/bash
# Your code will go here
echo "Script name: $0"
echo "First argument: $1"
echo "Second argument: $2"
echo "Third argument: $3"
##Output
labex:project/ $ chmod +x arguments.sh
##Below command where we pass the parameters, observe the output
labex:project/ $ ./arguments.sh hello world example
Script name: ./arguments.sh
First argument: hello
Second argument: world
Third argument: example
Passing argument with condition
#!/bin/bash
##Below '$#' is the special variable which contains the 'number of argument'
if [ $# -eq 0 ]; then
echo "No arguments provided."
elif [ $# -eq 1 ]; then
echo "One argument provided: $1"
elif [ $# -eq 2 ]; then
echo "Two arguments provided: $1 and $2"
else
echo "More than two arguments provided:"
echo "First argument: $1"
echo "Second argument: $2"
echo "Third argument: $3"
echo "Total number of arguments: $#"
fi
##Output
labex:project/ $ ./arguments.sh
No arguments provided.
labex:project/ $ ./arguments.sh one
One argument provided: one
labex:project/ $ ./arguments.sh one two
Two arguments provided: one and two
labex:project/ $ ./arguments.sh one two three four
More than two arguments provided:
First argument: one
Second argument: two
Third argument: three
Total number of arguments: 4
$#is a special variable that contains the number of arguments passed to the script.[ $# -eq 0 ]is a condition that checks if the number of arguments is equal to 0.elifis short for "else if". It allows you to check multiple conditions in sequence.The
-eqoperator means "equal to". There are other operators like-lt(less than),-gt(greater than), etc.
Loop through all arguments
#!/bin/bash
echo "Total number of arguments: $#"
echo "All arguments:"
## $@ is a special variable contains all the arguments, then there is a for loop
count=1
for arg in "$@"; do
echo "Argument $count: $arg"
count=$((count + 1))
done
##Output
labex:project/ $ ./arguments.sh apple banana cherry date
Total number of arguments: 4
All arguments:
Argument 1: apple
Argument 2: banana
Argument 3: cherry
Argument 4: date



