Introduction
In this lab, you will learn how to perform basic arithmetic operations in Shell programming. You'll create a simple script to calculate the total cost of a fruit basket, demonstrating the use of variables and arithmetic expressions in Bash. This lab is designed for beginners, so we'll explain each step in detail.
Create a new Bash script
Let's start by creating a new Bash script file.
Open your terminal in the WebIDE. You should see a command prompt, which might look something like this:
labex@ubuntu:~/project$.We'll create our script in the
projectdirectory. You're already in this directory by default, but let's make sure by using thecdcommand:cd ~/projectThis command changes the current directory to
/home/labex/project.Now, let's create a new file named
fruit_basket.sh. We'll use thetouchcommand, which creates an empty file:touch fruit_basket.shOpen the
fruit_basket.shfile in the WebIDE editor. You can do this by clicking on the file name in the file explorer on the left side of the WebIDE.Every Bash script should start with a "shebang" line. This line tells the system which interpreter to use to run the script. Add the following line at the beginning of the file:
#!/bin/bashThis line specifies that the script should be run with the Bash interpreter.
Define variables for fruit costs
Now that we have our script file, let's define some variables to store the costs of different fruits and the basket.
Add the following lines to your fruit_basket.sh file:
#!/bin/bash
## Define costs
COST_PINEAPPLE=50
COST_BANANA=4
COST_WATERMELON=23
COST_BASKET=1
Let's break this down:
- In Bash, we don't need to declare variables before using them. We simply assign a value to a variable name.
- Variable names are case-sensitive. By convention, we often use uppercase for constants (values that won't change).
- There should be no spaces around the
=sign when assigning values. - These values represent the cost in cents. For example,
COST_PINEAPPLE=50means a pineapple costs 50 cents. - We don't need to specify a data type. Bash treats these as strings by default, but will handle them as numbers when we perform arithmetic operations.
Calculate the total cost
Now that we have our costs defined, let's calculate the total cost of a fruit basket containing 1 pineapple, 2 bananas, and 3 watermelons.
Add the following line to your fruit_basket.sh file:
#!/bin/bash
## Define costs
COST_PINEAPPLE=50
COST_BANANA=4
COST_WATERMELON=23
COST_BASKET=1
## Calculate total cost
TOTAL=$((COST_PINEAPPLE + (COST_BANANA * 2) + (COST_WATERMELON * 3) + COST_BASKET))
Let's examine this new line:
$(( ))is Bash's syntax for arithmetic operations. Anything inside these double parentheses is treated as an arithmetic expression.- Inside the arithmetic expression, we don't need to use
$before variable names. - We're performing several operations:
COST_PINEAPPLE: The cost of 1 pineapple(COST_BANANA * 2): The cost of 2 bananas(COST_WATERMELON * 3): The cost of 3 watermelonsCOST_BASKET: The cost of the basket itself
- These values are all added together, and the result is stored in the
TOTALvariable.
Note: Bash only handles integer arithmetic. If we were dealing with dollars and cents, we'd need to use a tool like bc for floating-point arithmetic.
Display the total cost
To see the result of our calculation, we need to print the total cost. Add the following line to your fruit_basket.sh file:
#!/bin/bash
## Define costs
COST_PINEAPPLE=50
COST_BANANA=4
COST_WATERMELON=23
COST_BASKET=1
## Calculate total cost
TOTAL=$((COST_PINEAPPLE + (COST_BANANA * 2) + (COST_WATERMELON * 3) + COST_BASKET))
## Display the total cost
echo "Total Cost is $TOTAL cents"
Let's break down this new line:
echois a command that prints text to the terminal.- The text in quotes will be printed as-is, except for the
$TOTALpart. - When a variable name is preceded by
$inside a string, Bash replaces it with the variable's value. This is called variable expansion. - So if
TOTALis 128, the output will be "Total Cost is 128 cents".
Make the script executable and run it
Now that our script is complete, we need to make it executable and then run it.
In the terminal, make the script executable with the
chmodcommand:chmod +x ~/project/fruit_basket.shThis command changes the mode of the file, adding execute (
x) permission for the user.Now, let's run the script:
~/project/fruit_basket.shThis command tells Bash to execute our script. The
~/project/part specifies the path to our script.
You should see output similar to:
Total Cost is 128 cents
This output shows that the total cost of our fruit basket (1 pineapple, 2 bananas, 3 watermelons, and the basket itself) is 128 cents.
Summary
In this lab, you learned how to perform arithmetic operations using basic operators in Shell programming. You created a Bash script that calculates the total cost of a fruit basket by defining variables for individual costs and using arithmetic expressions to compute the total. You also learned how to make a script executable and run it from the command line.
Key points to remember:
- Bash scripts start with a shebang line (
#!/bin/bash). - Variables in Bash are assigned without spaces around the
=sign. - Arithmetic operations in Bash are performed inside
$(( )). - The
echocommand is used to print output. - Scripts need to be made executable with
chmod +xbefore they can be run.
These skills form the foundation for more complex shell scripting tasks and can be applied to various scenarios where you need to perform calculations within your scripts.



