如何回显数学表达式的结果并将其保存到Bash中的变量中

问题描述 投票:0回答:2

我需要将以下等式回显到命令行,并保存到变量中。到目前为止,我都没有做到。

5^0.16

我试过了什么

echo 'e(l(5)*.16)' | bc -l

以及

echo 'e(l(5)*.16)' | bc -l | read wcEXP
bash shell scripting
2个回答
5
投票

怎么样

wcExp=$(echo 'e(l(5)*.16)' | bc -l)
echo "$wcExp"

2
投票

如果使用Bash,则可以使用tee将输出复制到标准错误:

res=$(bc -l <<< 'e(l(5)*.16)' | tee /dev/stderr)

这将打印bc命令的输出,并将其存储在res中:

$ res=$(bc -l <<< 'e(l(5)*.16)' | tee /dev/stderr)
1.29370483333398597850
$ declare -p res
declare -- res="1.29370483333398597850"
© www.soinside.com 2019 - 2024. All rights reserved.