bash 为什么我们同时使用 echo 和 bc?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/13272131/
Warning: these are provided under cc-by-sa 4.0 license.  You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why do we use echo and bc together?
提问by jsp99
In Shell scripting (Linux/Ubuntu , Bash) , why do we use echo and bc commands together ? I am new to Shell scripting and have a basic understanding of pipes .
I know that bc is kind of a seperate language . How does the following statement actually work (Just an example) ?echo 5+6 | bc
在 Shell 脚本(Linux/Ubuntu、Bash)中,为什么我们同时使用 echo 和 bc 命令?我是 Shell 脚本的新手,对管道有基本的了解。我知道 bc 是一种独立的语言。下面的语句实际上是如何工作的(只是一个例子)?echo 5+6 | bc
回答by unwind
The first command (echo)  writes the expression "5+6" to its standard output. This is then piped to bc's standard input, read from there, and evaluated.
第一个命令 ( echo) 将表达式“5+6”写入其标准输出。然后将其传送到bc的标准输入,从那里读取并评估。
This is used since bcdoesn't take the expression as a direct input, it always reads from files and/or standard input.
之所以使用它,是因为bc它不将表达式作为直接输入,它总是从文件和/或标准输入中读取。
回答by higuaro
You can use that program combination for another set of powerful operations, for example you can convert from hexadecimal to binary like this
您可以将该程序组合用于另一组强大的操作,例如您可以像这样从十六进制转换为二进制
echo "ibase=16; obase=2; A15" | bc
It will print: 101000010101
它会打印: 101000010101
As for the process of echoing and using the |operator, it just make the output of the echocommand an input for the bcprogram, you can achieve the same using for example: bc <<< "5 + 2"
至于回显和使用|运算符的过程,它只是将echo命令的输出作为bc程序的输入,您可以使用例如:bc <<< "5 + 2"
bcdoes not read operations from command line arguments, instead it reads it from an input file or in an interactive session
bc不从命令行参数读取操作,而是从输入文件或交互式会话中读取它
Another example of this useful combination is the calculation of really big quantities, like:
这种有用组合的另一个例子是计算非常大的数量,例如:
echo "2^1024" | bc
A note about the <<<:it passes a string on a single line as an input file to the command, if the program reads its input from a file, with <<<you can convert a string to a "file" and then pass this "file" to the program.
关于以下内容的注意事项<<<:它将单行字符串作为输入文件传递给命令,如果程序从文件中读取其输入,<<<您可以将字符串转换为“文件”,然后将此“文件”传递给该程序。
回答by jlliagre
echo is not required here and can be replaced by an here document:
此处不需要 echo,可以用此处的文档替换:
bc <<%
5+6
%
or with modern shells:
或使用现代贝壳:
bc <<< 5+6
回答by zzapper
A little bit of text formatting
一些文本格式
  echo -n "pi=" ; bc -l <<< "scale=10; 4*a(1)"
pi=3.1415926532
圆周率 = 3.1415926532
回答by Tomek Wyderka
Before bccommand we can see very simple PIPE:
在bc命令之前我们可以看到非常简单的 PIPE:
|
The name pipeis very accurate! Like a normal pipe this one is transferring water from source to target. In computer science water is called dataor information.
管子的名字很准确!就像一根普通的管道,这个管道将水从源头输送到目标。在计算机科学中,水被称为数据或信息。
Like every good pipe, both endings of it are special. Through those endings we can connect pipe to other pipes, taps, joints, etc.
就像每支好烟斗一样,它的两个结局都很特别。通过这些末端,我们可以将管道连接到其他管道、水龙头、接头等。
One ending of this pipe is connected to bcwhich has matching ending. Bcis a big piece of software, so it has many different connection points for different pipes. Also for this simple |pipe.
该管道的一端连接到具有匹配结尾的bc。Bc是一个很大的软件,所以它有很多不同的管道连接点。也是为了这个简单| 管道。
On the other hand 5+6is not any piece of software. It is pure data/water. You can imagine what will happen if you pure water to one end of the pipe without fixed connection! Lack of water pressure...
另一方面,5+6不是任何软件。它是纯数据/水。您可以想象如果您将纯净水注入没有固定连接的管道一端会发生什么!水压不足...
We need some software which has good connection to that pipe. Echois very simple application, doing practically nothing, like decent echo should do... But it has basic and functional ending matching that simple pipe.
我们需要一些与该管道有良好连接的软件。Echo是一个非常简单的应用程序,几乎什么都不做,就像体面的 echo 应该做的那样......但它有基本的和功能性的结尾匹配那个简单的管道。

