0

I am not able to find why my code is not running. I am trying to send integers as the actual parameters to the add() function. As command substitution will substitutes the output of add() in the place of add 5 6.

#!/bin/bash

add () {
        echo `expr $1 + $2`
}

VAR= `add 5 6`
echo ${VAR:-"This is not defined"}
vivek gupta
  • 103
  • 1
  • 12
  • 1
    The only real reason to use `expr` is for regular-expression matching; prefer the arithmetic expression `$(( $1 + $2 ))` instead. – chepner Oct 26 '18 at 15:24

2 Answers2

1

Remove the space after = in your VAR command.

VAR=`add 5 6`
Mayank Porwal
  • 33,470
  • 8
  • 37
  • 58
1

There is a space after VAR=, you need to remove the space:

#!/bin/bash

add () {
        echo `expr $1 + $2`
}

VAR=`add 5 6`
echo ${VAR:-"This is not defined"}

Output is

11

https://www.tldp.org/LDP/abs/html/varassignment.html

ScottBro
  • 309
  • 1
  • 12