Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Assignment of parameters for functions in Shell Script

I recently started learning to code Shell Script and one thing that confuses me is the order of assignment of parameters.

This is the code I have confusion with

#!/bin/bash
Expression() 
{
    local num3=$1
    local num4=$2
    local num5=$3
    local sum=$(((num3-num4)+num5))
echo $sum
}
num1=5
num2=3
num3=7
sum=$(Expression num1 num2 num3)
echo "The result is $sum"

Instead of getting the output as

The result is 9

Since 5-3+7=9

I'm getting it as

The result is 7

Could anyone explain this please?

like image 339
Viswa Avatar asked Mar 05 '23 07:03

Viswa


2 Answers

The problem is with the expression

sum=$(Expression num1 num2 num3)

where you are passing literal values of num1, num2 and num3 as strings. In the example you have those variables are not passed as placeholders for the values defined, but as raw strings.

You need to pass them as actual placeholders containing those values, by putting a $ before the variable name

sum=$(Expression "$num1" "$num2" "$num3")

I guess the decision you've incorrectly made is, the arithmetic operator $((..)) construct in bash allows you to access variables without the $ prefix made. But for command substitution syntax $(..) requires the variable names to be prefixed with $, else only the literal value gets passed as you've seen here.

As for the value 7, it should be obvious that num3 is defined both global and local inside the function. So the it holds the value 5, so the operation 5-3+5 is performed over 5-3+7 which you expected.

like image 73
Inian Avatar answered Apr 06 '23 08:04

Inian


Proper code:

#!/bin/bash
Expression() 
{
    local num4=$1
    local num5=$2
    local num6=$3
    local sum=$((((num4-num5))+num6))
echo $sum
}
num1=5
num2=3
num3=7
sum=$(Expression num1 num2 num3)
echo "The result is $sum"

Output:

./script.sh
The result is 9

Why your code is not working? When you run your code with bash -x option you will see the difference:

$ bash -x ./script.sh
+ num1=5
+ num2=3
+ num3=7
++ Expression num1 num2 num3
++ local num3=num1
++ local num4=num2
++ local num5=num3
++ local sum=7
++ echo 7
+ sum=7
+ echo 'The result is 7'
The result is 7

num3 is declared as both local and global variable. Since the function is using local one the value 5 is used instead of 7 as you expected.

like image 29
Szczerba Avatar answered Apr 06 '23 08:04

Szczerba