我正在寻找一个可以在终端自身进行计算的计算器,不需要任何额外的前缀和后缀。
例如:如果我在终端中输入10000-9000,答案应该是1000。
再次强调,我只需要一个快速的终端计算器,不需要添加任何字符。我知道如果切换到Python,它可以做到这一点,但我不想以这种方式使用它。
例如:如果我在终端中输入10000-9000,答案应该是1000。
再次强调,我只需要一个快速的终端计算器,不需要添加任何字符。我知道如果切换到Python,它可以做到这一点,但我不想以这种方式使用它。
您可以使用calc
。它不是默认安装的,但是您可以使用以下命令快速安装:
sudo apt-get install apcalc
$ calc 5+2
7
$ calc 5-2
3
$ calc 5*2
10
$ calc 5/2
2.5
$ calc 5^2
25
$ calc 'sqrt(2)'
1.4142135623730950488
$ calc 'sin(2)'
0.9092974268256816954
$ calc 'cos(2)'
-0.416146836547142387
$ calc 'log(2)'
~0.30102999566398119521
$ calc 'sqrt(sin(cos(log(2))))^2'
~0.81633199125847958126
$ # and so on...
man calc
和calc help
说得很清楚了。 - Radu Rădeanucalc help builtin
来查看支持的函数列表,比如sin()
。 - Penghe Geng((...))
语法进行简单的整数运算,例如:$ echo $((10000-9000))
1000
bc
的计算器,它可以接受标准输入上的算术表达式。$ echo "10000-9000" | bc
1000
该bc
程序也可以进行浮点数算术运算。
$ echo "scale = 3; 0.1-0.09" | bc
.01
calc()
NL {
NL echo "$@" | bc
NL }
其中NL代表换行符。然后您可以输入calc 1234 + 768
(输出2002
),或者任何其他bc
能够解释的公式。当然您可以使用任何函数名称,我只是认为calc
与此功能的用途相匹配。 - usercalc() { echo "$@" | bc; }
calc 99 + -1001
返回 -902
- Hans Zcalc 3*2
会得到 6
,但执行 calc 3 * 2
则会导致解析错误。如果你真的想执行 calc 3 * 2
,你可以使用转义字符 \*
,即 calc 3 \* 2
。其他一些特殊字符如 <>=!&|
在调用时也需要单独进行转义,例如 calc 3 \> 2
1
。我还可以建议使用 calcf() { echo "$@" | bc -l; }
来进行浮点数计算,因为 bc 无法在整数和浮点数之间切换。 - Hans Z$((4-5))
和$[4-5]
之间有任何区别? - aidanecho
:bc <<< "2*2"
- Bernhardexpr 1 + 2
比echo $((1 + 2))
更容易输入,效果是一样的。(当你使用特殊的shell字符如*
和括号时,差异就会显现出来,在这种情况下,你需要转义或者无论如何都要使用$((...))
:expr \( 1 + 2 \) \* 3
或者echo $(((1 + 2) * 3))
。bc
的额外功能包括:任意精度计算(bc <<<scale=2\;1/3
得到.33
);进制转换:bc <<<ibase=8;obase=16;30
得到1A
(八进制的30转换成十六进制是1A)。 - Lekensteyn另一个可能的解决方案是为Bash的内置算术添加一个简单的函数。将其放入你的.bashrc
文件中以尝试:
=() {
echo "$(($@))"
}
所以现在,你甚至不再需要$((...))
,只需要=
就足够自然了。
如果你想更快一些,还有另外一件事:你可以将p
替换为+
,将x
替换为*
。这样就可以实现:
=() {
local IFS=' '
local calc="${*//p/+}"
calc="${calc//x/*}"
echo "$(($calc))"
}
= 5 x 5 # Returns 25
= 50p25 # Returns 75
=
。
如果需要的话,输出可以同时显示为十进制和十六进制。(注意:使用x
替代会与0x...
的十六进制语法冲突)
=() {
local answer="$(($@))"
printf '%d (%#x)\n' "$answer" "$answer"
}
例子:
$ = 16 + 0x10
272 (0x110)
$ = 16**3 + 16**4
69632 (0x11000)
bc
如果你想进行稍微高级一点的计算,你可以将其通过管道传递给 bc
,像这样:
=() {
local IFS=' '
local calc="${*//p/+}"
calc="${calc//x/*}"
bc -l <<<"scale=10;$calc"
}
= 'sqrt(2)' # Returns 1.4142135623
= '4*a(1)' # Returns pi (3.1415926532)
bc
提供的功能如下(可在 man bc
中找到):sqrt ( expression )
The value of the sqrt function is the square root of the expression.
If the expression is negative, a run time error is generated.
s (x) The sine of x, x is in radians.
c (x) The cosine of x, x is in radians.
a (x) The arctangent of x, arctangent returns radians.
l (x) The natural logarithm of x.
e (x) The exponential function of raising e to the value x.
j (n,x)
The Bessel function of integer order n of x.
if
、for
、while
和变量。不过,如果你想要写入文件,最好是将其写入文件。p
和x
。最好是直接删除这些替换。
gcalccmd
gcalccmd
(来自gnome-calculator
):=() {
local IFS=' '
local calc="$*"
# Uncomment the below for (p → +) and (x → *)
#calc="${calc//p/+}"
#calc="${calc//x/*}"
printf '%s\n quit' "$calc" | gcalccmd | sed 's:^> ::g'
}
= 'sqrt(2)' # Returns 1.4142135623
= '4^4' # Returns 256
==
表示等效函数:ln()
sqrt()
abs()
int()
frac()
sin()
cos()
tan()
sin⁻¹() == asin()
cos⁻¹() == acos()
tan⁻¹() == atan()
sinh()
cosh()
tanh()
sinh⁻¹() == asinh()
cosh⁻¹() == acosh()
tanh⁻¹() == atanh()
ones()
twos()
gcalccmd
命令效果不太好。它显示了错误3
,可能是因为那个多余的空格。然而,稍作修改后的命令对我来说完美地运行了:echo -e "$calc\nquit"| gcalccmd | sed "s:^> ::g"
。 - udiboy1209function = { R -q --vanilla -e "$@" | grep -E '[^>](.*)' -o -x --color=never }
- Morteza Milani= 2 + 0x20
34 (0x22)
- FractalSpacec(){ python -c "from math import *; print($*, hex($*))" }
- FractalSpacebc
是一个非常非常烦人的软件。 - jwg=2048 / 3.1
。 - Frank N如果你的意思是,只要加载终端就可以做到这一点...
$ 10000 - 9000 1000 $
... 那么不,仅仅通过终端而没有其他任何工具是无法实现这个的,因为Bash不处理这样的数值参数。
ipython
比标准的Python shell要更加有用。 - Naftuli Kay.bashrc
中可以像这样写:calc() {
python3 -c 'import sys; print(eval(" ".join(sys.argv[1:])))' "$@"
}
calc 5 + 5
# Returns 10
result="$(calc 5+5)"
# Stores the result into a variable
math
模块函数的方法。(点击这里获取更多信息)calc() {
python3 -c 'from math import *; import sys; print(eval(" ".join(sys.argv[1:])))' "$@"
}
calc 'sqrt(2)' # Needs quotes because (...) is special in Bash
# Returns 1.4142135623730951
result="$(calc 'sqrt(2)')"
# Stores the result into a variable
**
表示乘方,用%
表示取模)calc
,#!/usr/bin/python3
from math import *
import sys
print(eval(' '.join(sys.argv[1:])))
c
、?
或者 +
?),这样每次打开终端时都会有它。 - kiripython2 -c "from __future__ import division; from math import *; print($*)"
,否则它将始终执行整数除法。例如,对于calc 5/2
,你将得到2
而不是2.5
。参考链接:http://stackoverflow.com/a/183870/202504 - jmiserezpi
和e
。我觉得将其作为一个独立的脚本更加灵活和可移植。这里有一个简单的要点:https://gist.github.com/jasongeng/279eb396c01e74beb9ef - Penghe Genggnome-calculator
(>=13.04)或gcalctool
(<13.04)软件包中的gcalccmd
命令。我认为该软件包默认已安装。% gcalccmd
> 2+3
5
> 3/2
1.5
> 3*2
6
> 2-3
−1
>
man gcalccmd
说:一个控制台计算器。简单来说,这正是OP想要的.. :) - Saurav Kumar./bash_aliases
中是一个好主意,如alias calc='gcalccmd'
。或者,如果你想进行即时计算(带有即时答案),可以添加alias calc='gnome-calculator -s'
- 后者的用法是:calc 2000/1300
或calc 2+3
。在这种情况下,无需退出计算器 - 它会直接给出答案,无需输入quit
。 - Rafalsudo apt-get install qalc
sudo apt-get install qalculate-gtk
对于图形用户界面(GUI)。
它具有许多功能,例如:
20 m / s * 12 h = 864 kilom
pi
、e
、c
、avogadro
sin(pi) = 0
、gamma(4) = 6
、5! = 120
、log(1024, 2) = 10
> 120 in
120 * inch = 120 in
> convert cm
120 in = 304.8 centim
(x + y)^2 = x^2 + 2xy + y^2
integrate 3*x^2 = x^3
,diff sin(x), pi
help convert
,help integrate
factorial(5)
和 faculteit(5)
。你说你想要不使用前缀来使用它,嗯...你可以使用一个前缀:
$ qalc 5 ft + 3 cm (5 * foot) + (3 * centim) = 1.554 m
也可以将其作为 REPL 运行。
#!/bin/bash
echo "$@" | bc
# nano /bin/c
# chmod +x /bin/c
$ c 10000-9000
1000
echo "$@" | bc
,这样你可以更自然地在计算中添加空格。 - kiri=
代替c
作为该命令的名称怎么样?Bash实际上允许您将其用作函数的名称。 - Kaz~/bin
(你的主目录下的 bin
)中,并确保 /home/yourname/bin
在你的 PATH
中。 - Kaz.bashrc
中定义一个函数就足够了。 - kiri/etc/bash.bashrc
(在Ubuntu 10.04上),它将修改command_not_found
处理程序,如果未知命令的第一个字符是数字或-
或+
,则运行shell的表达式求值器。*
,您将需要使用\
或引号引用*
,因为shell会在决定要运行哪个命令之前执行文件名扩展。其他操作符如>>
也是同样的情况。~/.bashrc
中,然后键入. ~/.bashrc
并尝试一下。# if the command-not-found package is installed, use it
if [ -x /usr/lib/command-not-found -o -x /usr/share/command-not-found ]; then
function command_not_found_handle {
if [[ $1 == [0-9+-]* ]]; then
echo $(( $@ ))
elif [ -x /usr/lib/command-not-found ]; then
/usr/bin/python /usr/lib/command-not-found -- $1
return $?
elif [ -x /usr/share/command-not-found ]; then
/usr/bin/python /usr/share/command-not-found -- $1
return $?
else
return 127
fi
}
fi
cta
,这是一个拼写错误,只是为了测试我们的新的命令未找到处理程序是否仍会尝试查找未知命令)。mp@ubuntu:~$ cta
No command 'cta' found, did you mean:
Command 'cda' from package 'xmcd' (universe)
Command 'cat' from package 'coreutils' (main)
cta: command not found
mp@ubuntu:~$ 9000-1000
8000
echo $(( $@ ))
这一行改为运行其他回答者提到的更高级的计算器程序之一,这些程序接受命令行参数。 - Mark Plotnickdc
!这是核心工具的一部分,所以在OS X、Ubuntu和几乎所有其他操作系统中都已安装。这是一个逆波兰计算器,所以如果你不喜欢这种类型的计算器,那它就不适合你。
非常基本的命令如下(手册中包含了我没有列出来的语法,有指数运算哦!)
只需要在数字之间加上空格即可。在其他情况下,空格将被忽略。
输入一个数字会将其推入堆栈顶部。
+ Adds top 2 items in stack, then pushes result to stack (`2 4 +p` outputs 6)
- Subtracts top 2 items in stack, then pushes result to stack (`4 2 -p` outputs 2)
* Multiplies top 2 items in stack, then pushes result to stack (`6 5 *p` outputs 30)
/ Divides top 2 items in stack, then pushes result to stack (`54 7 /p` outputs 8)
p Print top item in stack, without destroying it
c Clear stack
r Swap top 2 items on stack
d Duplicate top item on stack
k Pops top item off stack, using it to determine precision (so 10 k would print 10 numbers after the decimal point). Default is 0, so it won't do floating point math by default.
n Pops top value off stack, then sends to stdout without a trailing newline
f Dump stack. Useful for finding what something does
42 * 5
,然后输出答案。 - Yet Another User1+1
几乎永远不会起作用。所以你的答案是正确的。 - kiri
command_not_found
钩子来处理,如果命令看起来像是数学相关的,尝试将其传递给bc
/calc
或其他类似的工具。不过,这种方法仍然有些不太正规。 - chronitis10000-9000
,而不运行任何程序或输入其他命令吗? - jumpnett