linuxbash

How to assign the result of 'echo' to a variable in a Bash script


I have copied a Bash script that extracts values from an XML file.

I can 'echo' the result of a calculation perfectly, but assigning this to a variable doesn't seem to work.

#!/bin/bash
IFS=$'\r\n' result=(`curl -s "http://xxx.xxx.xxx.xxx/smartmeter/modules" | \
xmlstarlet sel -I -t -m "/modules/module" \
    -v "cumulative_logs/cumulative_log/period/measurement" -n \
    -v "point_logs/point_log/period/measurement" -n | \
sed '/^$/d' `)

# Uncomment for debug
echo "${result[0]}"*1000 |bc
gas=$(echo"${result[0]}"*1000 |bc)

echo "${result[0]}"*1000 |bc

Gives me the result I need, but I do not know how to assign it to a variable.

I tried with tick marks:

gas=\`echo"${result[0]}"*1000 |bc\`

And with $(

How can I make it work?


Solution

  • If you want to use bc anyway, then you can just use backticks. Why are you using the backslashes?

    This code works; I just tested it.

      gas=`echo ${result[0]}*1000 | bc`
    

    Use one space after echo and no space around the * operator.