linux echo命令_如何在Linux上使用Echo命令

linux echo命令

linux echo命令

A Linux terminal window on a Ubuntu-themed desktop.
Fatmawati Achmad Zaenuri/Shutterstock Fatmawati Achmad Zaenuri / Shutterstock

The echo command is perfect for writing formatted text to the terminal window. And it doesn’t have to be static text. It can include shell variables, filenames, and directories. You can also redirect echo to create text files and log files. Follow this simple guide to find out how.

echo命令非常适合将格式化的文本写入终端窗口。 而且它不必是静态文本。 它可以包括外壳程序变量,文件名和目录。 您还可以重定向echo以创建文本文件和日志文件。 遵循此简单指南以了解操作方法。

回声重复您告诉它重复的内容 (Echo Repeats What You Tell It To Repeat)

Zeus was fond of leaving Mount Olympus to consort with beautiful nymphs. On one trip, he told a mountain nymph called Echo to waylay his wife, Hera, if she followed him. Hera did come looking for Zeus, and Echo did all she could to keep Hera in conversation. Finally, Hera lost her temper and cursed poor Echo so that she only repeat the last words that someone else had said. What Hera did to Zeus when she caught up with him is anybody’s guess.

宙斯喜欢离开奥林匹斯山与美丽的仙女结伴。 在一次旅行中,他告诉一个叫Echo的山若虫,如果她的妻子赫拉(Hera)跟着他的话,要杀死他。 Hera确实来找宙斯,而Echo竭尽所能让Hera保持对话。 最后,赫拉发脾气并诅咒可怜的回声,因此她只重复别人说的最后一句话。 赫拉在追赶宙斯时对宙斯所做的事是所有人的猜测。

And that, pretty much, is echo‘s lot in life. It repeats what it has been told to repeat. That’s a simple function, but a vital one. Without echo , we’d be unable to get visible output from shell scripts, for example.

这几乎就是echo的生命。 它重复了被告知要重复的内容 。 这是一个简单的功能,但至关重要。 例如,如果没有echo ,我们将无法从shell脚本获得可见的输出。

Whilst not laden down with a multitude of bells and whistles, there’s a good chance that echo has some capabilities that you didn’t know about or that you’d forgotten.

虽然不会沉迷于繁琐的事情,但echo很有可能具有一些您不知道或忘记的功能。

回声? 回声! (echo? echo!)

Most Linux systems provide two versions of echo. The Bash shell has its own echo built into it, and there’s a binary executable version of echo as well.

大多数Linux系统提供echo两个版本。 Bash shell内置了自己的echo ,并且还有echo的二进制可执行版本。

We can see the two different versions by using the following commands:

通过使用以下命令,我们可以看到两个不同的版本:

type echo
whereis echo
type echo in a terminal window

The type command tells us whether the command we pass to it as its argument is a shell builtin, a binary executable, an alias, or a function. It reports to us that echo is a shell builtin.

type命令告诉我们作为参数 传递给它的命令是内置的shell,二进制可执行文件,别名还是函数。 它向我们报告说echo是内置的shell。

As soon as it has found an answer, type stops looking for further matches. So it doesn’t tell us if there are other commands with the same name present in the system. But it does tell us which one it finds first. And that’s the one that will be used by default when we issue that command.

一旦它已经找到了答案, type停止寻找更多的匹配。 因此,它不会告诉我们系统中是否存在其他具有相同名称的命令。 但是它确实告诉我们它首先找到哪个。 这就是我们发出该命令时默认使用的那个。

The whereis command looks for the binary executable, source code, and man page for the command we pass to it as its command-line parameter. It doesn’t look for shell builtins because they don’t have a separate binary executable. They’re an integral part of the Bash executable.

whereis命令将查找二进制可执行文件,源代码和手册页,以查找我们作为命令行参数传递给它的命令。 它不查找shell内置程序,因为它们没有单独的二进制可执行文件。 它们是Bash可执行文件不可或缺的一部分。

The whereis command reports that echo is a binary executable located in the /bin directory.

whereis命令报告echo是位于/bin目录中的二进制可执行文件。

To use that version of echo you would need to explicitly call it by providing the path to the executable on the command line:

要使用该版本的echo您需要通过在命令行上提供可执行文件的路径来显式调用它:

/bin/echo --version
/bin/echo --version in a terminal window

The shell builtin doesn’t know what the --version command-line argument is, it just repeats it in the terminal window:

内置的shell不知道--version命令行参数是什么,它只是在终端窗口中重复它:

echo --version
echo --version in a terminal window

The examples shown here all use the default version of echo, in the Bash shell.

此处显示的示例全部在Bash shell中使用echo的默认版本。

向终端写文本 (Writing Text to the Terminal)

To write a simple string of text to the terminal window, type echo and the string you want it to display:

要将简单的文本字符串写入终端窗口,请键入echo和您希望其显示的字符串:

echo My name is Dave.
echo My name is Dave. in a terminal window

The text is repeated for us. But as you experiment, you’ll soon discover that things can get slightly more complicated. Look at this example:

为我们重复了这段文字。 但是,当您进行实验时,您很快就会发现事情可能会变得稍微复杂一些。 看这个例子:

echo My name is Dave and I'm a geek.
echo My name is Dave and I'm a geek. in a terminal window

The terminal window displays a  > sign and sits there, waiting. Ctrl+C will return you to the command prompt. What happened there?

终端窗口显示>符号,坐在那里等待。 Ctrl + C将使您返回命令提示符。 那里发生了什么?

The single quote or apostrophe in the word “I’m” confused echo. It interpreted that single quote as the start of a quoted section of text. Because it didn’t detect a closing single quote, echo was waiting for more input. It expected that further input to include the missing single quote it was waiting for.

“我是”一词中的单引号或撇号使echo混淆了。 它将单引号解释为文本带引号的部分的开头。 因为它没有检测到结束的单引号,所以echo正在等待更多输入。 它期望进一步的输入包括它正在等待的缺少单引号。

To include a single quote in a string, the simplest solution is to wrap the whole string within double quote marks:

要在字符串中包含单引号,最简单的解决方案是将整个字符串用双引号引起来:

echo "My name is Dave and I'm a geek."
echo "My name is Dave and I'm a geek." in a terminal window

Wrapping your text in double quote marks is good general advice. In scripts, it cleanly delimits the parameters you’re passing to echo. This makes reading—and debugging—scripts much easier.

用双引号引起来的文字是很好的一般建议。 在脚本中,它清楚地分隔了您传递给echo的参数。 这使阅读和调试脚本变得更加容易。

What if you want to include a double quote character in your string of text? That’s easy, just put a backslash \ in front of the double quote mark (with no space between them).

如果要在文本字符串中包含双引号字符怎么办? 这很容易,只需在双引号之前放置一个反斜杠\ (它们之间没有空格)。

echo "My name is Dave and I'm a \"geek.\""
echo "My name is Dave and I'm a \"geek.\"" in a terminal window

This wraps the word “geek” in double quote marks for us. We’ll see more of these backslash-escaped characters shortly.

这将“ geek”一词用双引号引起来。 不久我们将看到更多这些反斜杠转义的字符。

将变量与echo一起使用 (Using Variables With echo)

So far, we’ve been writing predefined text to the terminal window. We can use variables with echo to produce output that is more dynamic and has values inserted into it for us by the shell. We can define a simple variable with this command:

到目前为止,我们已经在终端窗口中写入了预定义的文本。 我们可以将变量与echo一起使用,以产生更具动态性的输出,并通过shell为我们插入值。 我们可以使用以下命令定义一个简单的变量:

my_name="Dave"

A variable called my_name has been created. It has been assigned the value of the text “Dave.” We can use the variable name in the strings that we pass to echo , and the value of the variable will be written to the terminal window. You must put a dollar sign $ in front of the variable name to let echo know it is a variable.

已创建一个名为my_name的变量。 已为其分配了文本“ Dave”的值。 我们可以在传递给echo的字符串中使用变量名,变量的值将被写入终端窗口。 您必须在变量名称前加一个美元符号$ ,以使echo知道它是一个变量。

There is a caveat. If you’ve wrapped your string in single quote marks echo will treat everything literally. To have the variable value displayed, and not the name of the variable, use double quote marks.

有一个警告。 如果您将字符串用单引号引起来, echo将按字面意义对待所有内容。 要显示变量而不是变量名称 ,请使用双引号。

echo 'My name is $my_name'
echo "My name is $my_name"
echo 'My name is $my_name' in a terminal window

Somewhat aptly, that’s worth repeating:

适当地,值得重复一遍:

  • Using single quote marks results in the text being written to the terminal window in a literal fashion.

    使用文本引号结果被写入到终端窗口中文字的方式。

  • Using double quote marks results in the variable being interpreted—also called variable expansion—and the value is written to the terminal window.

    使用引号引起变量被解释(也称为变量扩展),并且该被写入终端窗口。

将命令与echo一起使用 (Using Commands With echo)

We can use a command with echo and incorporate its output into the string that is written to the terminal window. We must use the dollar sign $ as though the command was a variable, and wrap the whole command in parentheses.

我们可以将命令与echo ,并将其输出合并到写入终端窗口的字符串中。 我们必须像命令是变量一样使用美元符号$ ,并将整个命令括在括号中。

We’re going to use the date command. One tip is to use the command on its own before you start using it with echo. That way, if there is something wrong with the syntax of your command, you identify it and correct it before you include it in the echo command. Then, if the echo command doesn’t do what you expect, you’ll know the issue must be with the echo syntax because you’ve already proven the command’s syntax.

我们将使用date命令 。 一个技巧是在通过echo开始使用它之前,先单独使用该命令。 这样,如果命令的语法有问题,则可以在将其包含在echo命令中之前对其进行识别和更正。 然后,如果echo命令没有按预期执行操作,您将知道问题一定出在echo语法上,因为您已经证明了该命令的语法。

So, try this in the terminal window:

因此,请在终端窗口中尝试以下操作:

date +%D
date +%D in a terminal window

And, satisfied that we’re getting what we expect from the date command, we’ll integrate it into an echo command:

并且,满足于我们从date命令中获得了期望,我们将其集成到echo命令中:

echo "Today's date is: $(date +%D)"
echo "Today's date is: $(date +%D)" in a terminal window

Note the command is inside the parentheses and the dollar sign $ is immediately before the first parenthesis.

请注意,该命令在括号内,并且美元符号$在第一个括号之前。

用回声格式化文本 (Formatting Text With echo)

The -e (enable backslash escapes) option lets us use some backslash-escaped characters to change the layout of the text. These are the backslash-escaped characters we can use:

-e (启用反斜杠转义)选项使我们可以使用一些反斜杠转义的字符来更改文本的布局。 这些是我们可以使用的反斜杠转义字符:

  • \a: Alert (historically known as BEL). This generates the default alert sound.

    \ a :警报(历史上称为BEL)。 这将生成默认的警报声音。

  • \b: Writes a backspace character.

    \ b :写入一个退格字符。

  • \c: Abandons any further output.

    \ c :放弃任何进一步的输出。

  • \e: Writes an escape character.

    \ e :写一个转义字符。

  • \f: Writes a form feed character.

    \ f :写入换页符。

  • \n: Writes a new line.

    \ n :写一个新行。

  • \r: Writes a carriage return.

    \ r :写回车符。

  • \t: Writes a horizontal tab.

    \ t :写入水平制表符。

  • \v: Writes a vertical tab.

    \ v :写入垂直制表符。

  • \\: Writes a backslash character.

    \\ :写一个反斜杠字符。

Let’s use some of them and see what they do.

让我们使用其中一些,看看它们的作用。

echo -e "This is a long line of text\nsplit across three lines\nwith\ttabs\ton\tthe\tthird\tline"
echo -e "This is a long line of text\nsplit across three lines\nwith\ttabs\ton\tthe\tthird\tline" in a terminal window

The text is split into a new line where we’ve used the \n characters and a tab is inserted where we’ve used the \t characters.

文本被拆分为新行,在其中使用\n字符,并在使用\t字符的位置插入标签。

echo -e "Here\vare\vvertical\vtabs"
echo -e "Here\vare\vvertical\vtabs" in a terminal window

Like the \n new line characters, a vertical tab \v moves the text to the line below. But, unlike the \n new line characters, the \v vertical tab doesn’t start the new line at column zero. It uses the current column.

\n新行字符一样,垂直标签\v将文本移到下面的行。 但是,与\n新行字符不同, \v垂直选项卡不会在零列开始新行。 它使用当前列。

The \b backspace characters move the cursor back one character. If there is more text to be written to the terminal, that text will overwrite the previous character.

\b退格字符将光标移回一个字符。 如果有更多文本要写入终端,则该文本将覆盖前一个字符。

echo -e "123\b4"
echo -e "123\b4" in a terminal window

The “3” is over-written by the “4”.

“ 3”被“ 4”覆盖。

The \r carriage return character causes echo to return to the start of the current line and to write any further text from column zero.

\r回车符使echo返回到当前行的开头,并写入零列中的任何其他文本。

echo -e "123\r456"
echo -e "123\r456" in a terminal window

The “123” characters are overwritten by the “456” characters.

“ 123”字符被“ 456”字符覆盖。

The \a alert character will produce an audible “bleep.” It uses the default alert sound for your current theme.

\a警报字符将产生可听见的“哔哔”声。 它使用当前主题的默认警报声音。

echo -e "Make a bleep\a"
echo -e "Make a bleep\a" in a terminal window

The -n (no newline) option isn’t a backslash-escaped sequence, but it does affect the cosmetics of the text layout, so we’ll discuss it here. It prevents echo from adding a newline to the end of the text. The command prompt appears directly after the text that is written to the terminal window.

-n (无换行符)选项不是反斜杠转义的序列,但是它确实会影响文本布局的外观,因此我们将在这里讨论。 它可以防止echo将换行符添加到文本末尾。 命令提示符直接出现在写入终端窗口的文本之后。

echo -n "no final newline"
echo -n "no final newline" in a terminal window

在文件和目录中使用echo (Using echo With Files and Directories)

You can use echo as a sort of poor man’s version of ls. Your options are few and far between when you use echo like this. If you need any kind of fidelity or fine control, you’re better off using ls and its legion of options.

您可以将echo用作ls的可怜人版本。 像这样使用echo时,您的选择很少。 如果您需要任何一种保真度或精细控制,最好使用ls 及其众多选择

This command lists all of the files and directories in the current directory:

此命令列出当前目录中的所有文件和目录:

echo *

This command lists all of the files and directories in the current directory whose name starts with “D” :

此命令列出名称以“ D”开头的当前目录中的所有文件和目录:

echo D*

This command lists all of the “.desktop” files in the current directory:

此命令列出当前目录中的所有“ .desktop”文件:

echo *.desktop
echo * in a terminal window

Yeah. This isn’t playing to echo‘s strengths. Use ls.

是的 这并不是在发挥echo的作用。 使用ls

回显写入文件 (Writing to Files with echo)

We can redirect the output from echo and either create text files or write into existing text files.

我们可以重定向echo的输出,并创建文本文件或写入现有文本文件。

If we use the > redirection operator, the file is created if it does not exist. If the file does exist, the output from echo is added at the start of the file, overwriting any previous content.

如果我们使用>重定向运算符,则如果文件不存在,则会创建该文件。 如果文件确实存在,则echo的输出将添加到文件的开头,从而覆盖以前的所有内容。

If we use the >> redirection operator, the file is created if it does not exist. The output from echo is added to the end of the file and doesn’t overwrite any existing content of the file.

如果我们使用>>重定向运算符,则创建该文件(如果不存在)。 echo的输出将添加到文件的末尾,并且不会覆盖文件的任何现有内容。

echo "Creating a new file." > sample.txt
echo "Adding to the file." >> sample.txt
cat sample.txt
echo "Creating a new file." > sample.txt in a terminal window

A new file is created by the first command, and text is inserted into it. The second command adds a line of text to the bottom of the file. The cat command displays the contents of the file to the terminal window.

第一个命令创建一个新文件,并将文本插入其中。 第二个命令在文件底部添加一行文本。 cat命令将文件的内容显示到终端窗口。

And of course, we can include variables to add some useful information to our file. If the file is a logfile, we might want to have a timestamp added to it. We can do that with the next command.

当然,我们可以包含变量以向文件添加一些有用的信息。 如果该文件是日志文件,则可能需要添加时间戳。 我们可以使用下一个命令来做到这一点。

Note the single quote marks around the parameters for the date command. They prevent the space between the parameters being interpreted as the end of the parameter list. They ensure the parameters are passed to date correctly.

注意date命令的参数周围的单引号。 它们可以防止将参数之间的空格解释为参数列表的末尾。 他们确保参数传递给date正确。

echo "Logfile started: $(date +'%D %T')" > logfile.txt
cat logfile.txt
echo "Logfile started: $(date +'%D %T')" > logfile.txt in a terminal window

Our logfile is created for us and cat shows us that the datestamp and timestamp were both added to it.

我们为我们创建了日志文件,并且cat向我们显示了datestamp和timestamp均已添加到其中。

那是回声的曲目 (That’s echo’s Repertoire)

A simple command, but indispensable. If it didn’t exist, we’d have to invent it.

一个简单的命令,但必不可少。 如果它不存在,我们就必须发明它。

Zeus’s shenanigans did some good, after all.

毕竟,宙斯的恶作剧起到了一些作用。

翻译自: https://www.howtogeek.com/446071/how-to-use-the-echo-command-on-linux/

linux echo命令

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值