Simple Bash Script ๐Ÿ“œ Subtracting an Integer from a Variable

Learn how to subtract an integer from a variable in a bash script โž– Four methods, including (()), $(( )), expr, declare, and the bc command

Subtracting Variables in Bash Scripts

Bash is a powerful shell and scripting language found on most Linux and UNIX-based operating systems. Scripts written in Bash allow automation of repetitive tasks, chained commands, and more complex logic flows. A common operation in Bash scripts is manipulating variables and performing arithmetic on them, such as subtracting one variable from another. This article will explore the various methods available in Bash to subtract two variables.

Can I Subtract Two Variables in Bash?

Yes, Bash provides multiple ways to subtract the value of one variable from another and store the result. The key options are:

  • The expr command
  • The declare builtin with -i
  • Arithmetic expansion using $((…))

Each approach has its own syntax and behaviors to be aware of.

Using the expr Command

The expr command can evaluate an expression and output the result. For subtracting variables, the syntax is:

result=$(expr $var1 - $var2)

This executes expr, substitutes the two variables, performs the subtraction, and stores the output in the result variable.

For example:

var1=15 var2=3 result=$(expr $var1 - $var2) echo $result # Outputs 12

The expr command works for integer arithmetic only. One caveat is that Bash interprets numbers with leading zeros as octal, which can cause errors.

Using the declare Builtin

Bash’s declare builtin can also perform integer math when invoked with the -i option:

declare -i result=$var1-$var2

This declares result as an integer variable and assigns the subtraction of $var1 and $var2 to it.

For example:

var1=15 var2=3 declare -i result=$var1-$var2 echo $result # Outputs 12

Like expr, declare -i is limited to integers.

Using Arithmetic Expansion

The most flexible method for subtracting variables is to use Bash’s arithmetic expansion syntax with $((…)):


This allows directly subtracting the two variables and storing the result in result.

For example:

var1=15 var2=3 result=$((var1-var2)) echo $result # Outputs 12

Arithmetic expansion handles both integers and floating point numbers. It also supports multiple operations without spawning external processes.

How Can I Subtract Two Variables in Bash?

Based on the three options outlined above, the best practice for subtracting two variables in Bash is using arithmetic expansion with $((…)). Here is a simple code snippet demonstrating its usage:

# Integer example var1=15 var2=3 result=$((var1-var2)) echo $result # 12 # Floating point example var1=15.5 var2=3.5 result=$((var1-var2)) echo $result # 12

The key advantages of arithmetic expansion are:

  • Supports integers and floats natively
  • Allows multiple operations like $((var1-var2*var3))
  • Avoids spawning external processes
  • Provides direct variable access without $ prefix
  • Handles operator precedence automatically

For inline arithmetic in scripts, $((…)) should be the go-to method for subtracting variables in Bash.

Are There Any Tricks for Subtraction in Bash?

There are a couple nuances to be aware of when subtracting variables in Bash:

  • Leading zeros cause issues due to octal interpretation. Prefix integers with 10# for base 10.
  • For older Bash versions, use shopt -s globasciiranges for proper ASCII collating order.
  • For portability, use printf instead of date for timestamps.
  • Handle negatives carefully. Prefix base to move sign out front.
  • Use ${var%%[!0-9]} to strip non-digits from a variable.
  • Prefer ((var++)) over $((var+1)) for incrementing variables.
  • Use bc for higher precision floating point arithmetic.
  • Handle null or unset variables to avoid unintended results.

Overall, being aware of Bash’s arithmetic nuances will help avoid unexpected errors. The safest approach is using arithmetic expansion with $((…)).


In summary, Bash offers various methods for subtracting two variables, including expr, declare -i, and arithmetic expansion. However, the $((…)) approach stands out as the most robust and recommended choice. This method is versatile, accommodating both integers and floats, eliminating the need for external processes, automatically handling operator precedence, and allowing for clean, inline code. By harnessing $((var1 – var2))), scripts can efficiently perform variable subtraction using Bash’s built-in arithmetic capabilities. However, it’s important to exercise caution regarding potential edge cases, such as leading zeros or null values, to prevent unintended behavior. When it comes to floating-point operations, Bash arithmetic is sufficient for most use cases, with the option of using bc when higher precision is required. All in all, arithmetic expansion is the optimal approach for subtracting variables in a Bash script, offering a powerful and versatile solution.

Android | Linux | SDL - Narrow Escape
Leave a Reply

;-) :| :x :twisted: :smile: :shock: :sad: :roll: :razz: :oops: :o :mrgreen: :lol: :idea: :grin: :evil: :cry: :cool: :arrow: :???: :?: :!: