Arithmetic Operations in Shell Programming

ShellShellBeginner
Practice Now

This tutorial is from open-source community. Access the source code

Introduction

In this lab, you will learn how to perform arithmetic operations in Shell programming using the basic arithmetic operators.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/BasicSystemCommandsGroup(["`Basic System Commands`"]) linux(("`Linux`")) -.-> linux/FileandDirectoryManagementGroup(["`File and Directory Management`"]) linux(("`Linux`")) -.-> linux/BasicFileOperationsGroup(["`Basic File Operations`"]) shell(("`Shell`")) -.-> shell/BasicSyntaxandStructureGroup(["`Basic Syntax and Structure`"]) shell(("`Shell`")) -.-> shell/VariableHandlingGroup(["`Variable Handling`"]) shell(("`Shell`")) -.-> shell/AdvancedScriptingConceptsGroup(["`Advanced Scripting Concepts`"]) shell(("`Shell`")) -.-> shell/SystemInteractionandConfigurationGroup(["`System Interaction and Configuration`"]) linux/BasicSystemCommandsGroup -.-> linux/echo("`Text Display`") linux/FileandDirectoryManagementGroup -.-> linux/cd("`Directory Changing`") linux/BasicFileOperationsGroup -.-> linux/chmod("`Permission Modifying`") shell/BasicSyntaxandStructureGroup -.-> shell/shebang("`Shebang`") shell/BasicSyntaxandStructureGroup -.-> shell/comments("`Comments`") shell/BasicSyntaxandStructureGroup -.-> shell/quoting("`Quoting Mechanisms`") shell/VariableHandlingGroup -.-> shell/variables_decl("`Variable Declaration`") shell/VariableHandlingGroup -.-> shell/variables_usage("`Variable Usage`") shell/AdvancedScriptingConceptsGroup -.-> shell/arith_expansion("`Arithmetic Expansion`") shell/AdvancedScriptingConceptsGroup -.-> shell/cmd_substitution("`Command Substitution`") shell/AdvancedScriptingConceptsGroup -.-> shell/subshells("`Subshells and Command Groups`") shell/SystemInteractionandConfigurationGroup -.-> shell/globbing_expansion("`Globbing and Pathname Expansion`") linux/FileandDirectoryManagementGroup -.-> linux/wildcard("`Wildcard Character`") subgraph Lab Skills linux/echo -.-> lab-153897{{"`Arithmetic Operations in Shell Programming`"}} linux/cd -.-> lab-153897{{"`Arithmetic Operations in Shell Programming`"}} linux/chmod -.-> lab-153897{{"`Arithmetic Operations in Shell Programming`"}} shell/shebang -.-> lab-153897{{"`Arithmetic Operations in Shell Programming`"}} shell/comments -.-> lab-153897{{"`Arithmetic Operations in Shell Programming`"}} shell/quoting -.-> lab-153897{{"`Arithmetic Operations in Shell Programming`"}} shell/variables_decl -.-> lab-153897{{"`Arithmetic Operations in Shell Programming`"}} shell/variables_usage -.-> lab-153897{{"`Arithmetic Operations in Shell Programming`"}} shell/arith_expansion -.-> lab-153897{{"`Arithmetic Operations in Shell Programming`"}} shell/cmd_substitution -.-> lab-153897{{"`Arithmetic Operations in Shell Programming`"}} shell/subshells -.-> lab-153897{{"`Arithmetic Operations in Shell Programming`"}} shell/globbing_expansion -.-> lab-153897{{"`Arithmetic Operations in Shell Programming`"}} linux/wildcard -.-> lab-153897{{"`Arithmetic Operations in Shell Programming`"}} end

Define variable costs

First, let's define the costs of different fruits and the cost of the basket.

Create a new file and save it with ~/project/operators.sh.

#!/bin/bash

COST_PINEAPPLE=50
COST_BANANA=4
COST_WATERMELON=23
COST_BASKET=1

Calculate the total cost

Next, we will calculate the total cost of a fruit basket, which contains 1 pineapple, 2 bananas, and 3 watermelons.

#!/bin/bash

COST_PINEAPPLE=50
COST_BANANA=4
COST_WATERMELON=23
COST_BASKET=1

## Calculate the total cost
TOTAL=$((COST_PINEAPPLE + (COST_BANANA * 2) + (COST_WATERMELON * 3) + COST_BASKET))

Display the total cost

Finally, let's display the total cost of the fruit basket.

#!/bin/bash

COST_PINEAPPLE=50
COST_BANANA=4
COST_WATERMELON=23
COST_BASKET=1

## Calculate the total cost
TOTAL=$((COST_PINEAPPLE + (COST_BANANA * 2) + (COST_WATERMELON * 3) + COST_BASKET))

## Display the total cost
echo "Total Cost is $TOTAL"
cd ~/project
chmod +x operators.sh
./operators.sh
Total Cost is 128

Summary

In this lab, you learned how to perform arithmetic operations using the basic arithmetic operators in Shell programming. By defining variables for the costs of various items and using appropriate arithmetic expressions, you were able to calculate the total cost of a fruit basket.

Other Shell Tutorials you may like