终端计算器_为终端创建自己的快速计算器功能

终端计算器

终端计算器_为终端创建自己的快速计算器功能

If you’re anything like me, you’ve always got a Terminal window open, and you probably have even assigned a hotkey to bring it up anytime. So why open up the boring calculator when you can solve equations right there on the command line?

如果您像我一样,总是会打开一个“终端”窗口,甚至可能已经分配了一个热键随时将其打开。 那么,当您可以在命令行上求解方程式时,为什么还要打开无聊的计算器呢?

This is a simple method for creating a calculator using the built-in function support in the Bash shell. Essentially all we’re doing is assigning the question mark to run the bc command with whatever arguments you type after it.

这是使用Bash shell中的内置功能支持创建计算器的简单方法。 本质上,我们要做的就是分配问号以使用在其后键入的任何参数来运行bc命令。

创建终端计算器功能 (Creating the Terminal Calculator Function)

To create the function, just paste in the following command into the Terminal…

要创建功能,只需将以下命令粘贴到终端中即可。

? () { echo "$*" | bc -l; }

? () { echo "$*" | bc -l; }

Now you can use it by simply typing a ? and then the math equation you want solved. If you’re going to use spaces, you should surround it in quotes, otherwise you’ll get an error like this:

现在,您只需输入?就可以使用它。 然后要求解的数学方程式。 如果要使用空格,则应将其用引号引起来,否则会出现如下错误:

终端计算器_为终端创建自己的快速计算器功能

You can also use it on Windows if you have Cygwin installed—you’ll need to make sure you’ve installed the bc package, of course.

如果已安装Cygwin,也可以在Windows上使用它-当然,您需要确保已安装bc软件包。

终端计算器_为终端创建自己的快速计算器功能

Since this is using the bc command, it’s pretty powerful.

由于此命令使用bc命令,因此功能非常强大。

使函数跨保存重新启动 (Making the Function Save Across Restarts)

If you’re using Linux or Cygwin under Windows, you’ll want to edit the following file and put the line at the bottom:

如果您在Windows下使用Linux或Cygwin,则需要编辑以下文件,并将该行放在底部:

vi ~/.bash_profile

vi〜/ .bash_profile

Close out your terminal, re-open, and the command should be enabled now.

关闭您的终端,重新打开,该命令应该立即启用。

终端计算器_为终端创建自己的快速计算器功能

Saving the Function in OS X

在OS X中保存功能

If you’re using OS X, you’ll need to go another route, at least in my testing—first, open up a Terminal, and then type in the following commands:

如果您使用的是OS X,至少在我的测试中,您需要走另外一条路-首先,打开一个终端,然后键入以下命令:

touch .profile

触摸.profile

open .profile

打开.profile

You’ll need to be in your user folder at the time, which is the default folder anyway for  new Terminal window.

此时,您需要将其保存在用户文件夹中,这仍然是新“终端”窗口的默认文件夹。

终端计算器_为终端创建自己的快速计算器功能

The open command will open up the new .profile file in Textedit, where you can paste in the line, save, and close.

open命令将在Textedit中打开新的.profile文件,您可以在其中粘贴行,保存并关闭。

终端计算器_为终端创建自己的快速计算器功能

Close out the Terminal, re-open it, and you should have the function available.

关闭终端,重新打开终端,您应该已经可以使用该功能。

翻译自: https://www.howtogeek.com/howto/41168/create-your-own-quick-calculator-function-for-the-terminal/

终端计算器

相关文章:

  • 2022-12-23
  • 2021-11-11
  • 2022-12-23
  • 2021-06-13
  • 2022-12-23
  • 2021-06-11
  • 2022-12-23
  • 2021-12-30
猜你喜欢
  • 2021-10-18
  • 2021-12-05
  • 2021-11-30
  • 2021-10-19
  • 2022-02-15
  • 2022-12-23
  • 2021-07-07
相关资源
相似解决方案