使用 bc(Unix 基本计算器)时如何使用括号?

How can I use brackets while using bc (Unix basic calculator)?

在我的 Ubuntu WSL 上使用 bc、UNIX 基本计算器时,我发现了这个奇怪的行为:

Prompt> echo (1+2)*3 | bc -l
-bash: syntax error near unexpected token `1+2'
Prompt> echo (1 + 2) * 3 | bc -l
-bash: syntax error near unexpected token `1'

起初,我以为这意味着bc不覆盖括号,但后来我这样做了:

Prompt> bc
bc 1.07.1
Copyright 1991-1994, 1997, 1998, 2000, 2004, 2006, 2008, 2012-2017 Free Software Foundation, Inc.
This is free software with ABSOLUTELY NO WARRANTY.
For details type `warranty'.
(1 + 2) * 3
9
(1+2)*3
9
^C
(interrupt) use quit to exit.
quit

所以bc支持括号的使用。

有人知道我如何使用带括号的算术表达式,同时使用 bc 作为单行支持工具(我的意思是,在像 (...| bc)?

在您的表达式周围使用引号,例如:

echo "(1 + 2) * 3" | bc -l