That is a great question! As mentioned briefly in the step, native Bash cannot handle decimals. It only understands integers (whole numbers).
If you try to perform a calculation that results in a decimal, Bash will simply "chop off" the decimal part (truncation). For example:
echo $(( 10 / 3 ))
# Result: 3 (The .333 is lost)
To handle decimals in a Linux environment, we usually use one of the following methods:
1. The "Cent" Strategy (The simplest way)
This is exactly what we are doing in this lab! Instead of using dollars (0.50), we use cents (50).
- Pros: Very fast, stays within native Bash.
- Cons: You have to remember to convert it back to a human-readable format ($0.50) at the very end.
2. Using bc (Basic Calculator)
bc is an external command-line utility that supports floating-point math. You "pipe" your math string into it:
# scale=2 tells bc to use two decimal places
echo "scale=2; 10 / 3" | bc
# Result: 3.33
3. Using awk
awk is another powerful text-processing tool built into Linux that handles decimals perfectly:
awk "BEGIN {print 10 / 3}"
# Result: 3.33333
4. Why doesn't Bash just include decimals?
Bash was designed primarily as a tool for system administration—things like managing files, moving directories, and launching other programs. For these tasks, you usually only need to count things (integers). For heavy math, shell script creators preferred to delegate that work to specialized tools like bc or awk rather than making the shell itself bulky.
For this lab, sticking to whole numbers (cents) is the best way to keep things simple and functional! Are you ready to continue with the script?