Bash Script Subtraction Example

Bash scripting is a powerful way to automate tasks in Linux and other Unix-like operating systems. One common operation in Bash scripting is arithmetic, including subtraction. In this article, we will explore a simple Bash script that performs subtraction on two numbers provided by the user and displays the result.

Prerequisites

  • Basic understanding of Bash scripting
  • Familiarity with variables and arithmetic operations in Bash
  • A Bash-compatible terminal
  • Optional: Basic knowledge of command-line operations

DID YOU KNOW?

Bash, which stands for “Bourne Again SHell,” was created by Brian Fox in 1987 as a free software replacement for the Bourne shell.

The Script

The following Bash script prompts the user for two integer values, subtracts the second value from the first, and then outputs the result:

#!/bin/bash

echo "Enter the first number:"
read first_number

echo "Enter the second number:"
read second_number

result=$((first_number - second_number))

echo "The result of $first_number - $second_number is: $result"

Step-by-Step Explanation

NOTE!

Ensure that you have the necessary permissions to execute the script by running ‘chmod +x your_script_name.sh’.

This section breaks down the script into understandable parts, explaining each section’s purpose:

  1. Shebang: The first line, #!/bin/bash, indicates that the script should be run in the Bash shell.
  2. Prompting for Input: The echo command is used to prompt the user for input, asking for two integers.
  3. Reading Input: The read command takes user input and stores it in the variables first_number and second_number.
  4. Performing the Subtraction: The subtraction operation is performed using $((…)) syntax, storing the result in the result variable.
  5. Outputting the Result: Finally, the script outputs the result to the user.

How to Run the Script

To execute the script, follow these steps:

  1. Open a terminal window.
  2. Navigate to the directory where your script is located using the cd command.
  3. Execute the script by running ./your_script_name.sh.

Conclusion

In this article, we demonstrated how to create a simple Bash script for subtraction. This basic script showcases how to work with user input, perform arithmetic operations, and display output, which are essential skills in Bash scripting.

FAQ

  1. How do I make my script executable?

    You can make your script executable by running the command chmod +x your_script_name.sh.

  2. What if I receive a syntax error?

    Check your script for typos, especially in variable names and syntax.

  3. Can I subtract decimal numbers with this script?

    No, this script only supports integer subtraction. For decimal calculations, you can use tools like bc.

  4. How can I modify the script to handle negative results?

    You can modify the script to check if second_number is greater than first_number and handle negative results accordingly.

Troubleshooting

Here are some common error messages you might encounter and how to address them:

  • Command not found: Ensure you are using the correct command and that Bash is installed.
  • Permission denied: You may need to adjust file permissions using chmod.
  • Unexpected token: Check your script for syntax errors or mismatched parentheses.