1

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.

2
  • You don't need the variable; just let the output of bc go to standard output instead of capturing it. calculate_bmi () { bc -l <<< "$1/($2*$2)*10000"; }. Commented Jan 11, 2018 at 13:21
  • The echo statement was only for debugging purposes, i think i need a variable since i am writing the value to a csv file together with other data. Anyway, it works as it should at the moment. Commented Jan 11, 2018 at 13:23

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.