macbug
macbug

Reputation: 121

Format a bash command in a script

in the shell i type:

bc -l <<< '90.8/(179*179)*10000'

and i get the correct output:

28.33869105208951030000

however i am not able to format this correct in a bash script:

calculate_bmi () {
    BMI="$(bc -l <<< '${1}/(${2}*${2})*10000)'"
    echo "${BMI}"
}

I get all kind of strange errors when i tried different experiments. Latest error is:

./wts.sh: command substitution: line 25: syntax error near unexpected token `('
./wts.sh: command substitution: line 25: `bc -l <<< ${1}/(${2}*${2})*10000)"'

Please help me.

Upvotes: 1

Views: 89

Answers (1)

arco444
arco444

Reputation: 22831

Write your function as below:

calculate_bmi () {
    BMI=$(bc -l <<< "${1}/(${2}*${2})*10000")
    echo "${BMI}"
}

You must use double quotes (rather than single) in order to interpolate the variables.

Upvotes: 2

Related Questions