In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/02 Report--
This article will explain in detail how to use the echo command in Linux. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.
The echo command is one of the most basic and commonly used commands in Linux. It outputs the string passed as a parameter to the standard output.
Linux echo command
The echo command is a shell built into Bash and is commonly used in shell scripts to display messages or output the results of other commands.
The syntax of the echo command is:
Echo [- neE] [ARGUMENTS]
The parameters of the echo command:
-n: when the-n option is specified, subsequent line breaks are suppressed; that is, they do not automatically wrap at the end.
-e: when the-e option is specified, the following backslash escape characters are interpreted:
●\\-insert a backslash (\) character.
●\ a-alarm (BEL)
●\ b-displays backspace characters.
●\ c-suppress any further output
●\ e-displays escape characters.
●\ f-displays the feed character.
●\ n-displays the new line.
●\ r-shows enter.
●\ t-displays the horizontal tab.
●\ v-displays the vertical tab.
-E: when the-E option is specified, the interpretation of escape characters is disabled, which is the default value.
There are several points to consider when using the echo command:
● replaces all variables, wildcard matches, and special characters before passing parameters to the echo command.
Although ● is not necessary, it is a good programming practice to enclose the arguments passed to the echo command in double or single quotes.
When ● uses single quotation marks (''), the literal value of each character contained in the quotation marks is preserved. Variables and commands will not be expanded.
An example of using the Linux echo command
Let's see how to use the echo command with the following example:
Example 1: displays a line of text on standard output.
Echo Hello, World!
Output:
Hello, World!
Example 2: display lines of text containing double quotation marks
To output double quotes, you need to enclose them in single quotes or escape them using backslash characters.
Echo 'Hello "Linuxize''
Or
Echo "Hello\" Linuxize\ ""
Output:
Hello "Linuxize"
Example 3: display lines of text that contain single quotation marks
To print single quotes, you need to enclose them in double quotes or use ANSI-C quotes.
Echo "I'm a Linux user."
Or
Echo $'I\'m a Linux user.'
Output:
I'm a Linux user.
Example 4: display messages containing special characters
Use the-e option to enable the parsing of escaped characters.
Echo-e "You know nothing, Jon Snow.\ n\ t-Ygritte"
Output:
You know nothing, Jon Snow. -Ygritte
Example 5: returns the names of all .php files in the current directory
Description: the echo command can be used with pattern matching characters, such as wildcards.
Echo The PHP files are: * .php
Output:
The PHP files are: index.php contact.php functions.php
Example 6: redirect to a file
We can use the >, > > operators to redirect the output to a file instead of displaying the output on the screen.
Echo-e'The only true wisdom is in knowing you know nothing.\ nSocrates' > > / tmp/file.txt
Description: if file.txt does not exist, it will be created. When you use >, the file is overwritten; when you use > >, the output is appended to the file.
Use the cat command to view the contents of the file like this:
Cat / tmp/file.txt
Output:
The only true wisdom is in knowing you know nothing.Socrates
Example 7: display variables
Echo can also display variables. For example, we want to display the name of the currently logged in user
Echo $USER
Output:
Linuxuser
Description: $USER is a shell variable.
Example 8: display command output
Use the $(command) expression to pass the command output as an argument to echo. The following command displays the current date:
Echo "The date is: $(date +% D)"
Output:
The date is: 04 Linux 18Compact 19 this is the way to use the echo command in Linux. I hope the above content can be of some help and can learn more. If you think the article is good, you can share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.