ICode9

精准搜索请尝试: 精确搜索
首页 > 系统相关> 文章详细

Echo Command in Linux

2022-07-17 14:01:12  阅读:168  来源: 互联网

标签:Command string echo command Linux output Echo


 

 

https://phoenixnap.com/kb/echo-command-linux

 

 

 

   

Introduction

The echo command is a built-in Linux feature that prints out arguments as the standard output. echo is commonly used to display text strings or command results as messages.

In this tutorial, you will learn about all the different ways you can use the echo command in Linux.

Echo command in Linux

Prerequisites

  • A system running Linux
  • Access to the terminal window/command line

Echo Command Syntax

The echo command in Linux is used to display a string provided by the user.

The syntax is:

echo [option] [string]

For example, use the following command to print Hello, World! as the output:

echo Hello, World!
Echo command syntax  

Note: Using the echo command without any option returns the provided string as the output, with no changes.

Echo Command Options

Use the --help argument to list all available echo command options:

/bin/echo --help
Using the --help argument to list the echo command options  

Note: Using the echo --help command returns --help as the output.

The echo command uses the following options:

  • -n: Displays the output while omitting the newline after it.
  • -E: The default option, disables the interpretation of escape characters.
  • -e: Enables the interpretation of the following escape characters:
    • \\: Displays a backslash character (\).
    • \a: Plays a sound alert when displaying the output.
    • \b: Creates a backspace character, equivalent to pressing Backspace.
    • \c: Omits any output following the escape character.
    • \e: The escape character, equivalent to pressing Esc.
    • \f: The form feed character, causes the printer to automatically advance to the start of the next page.
    • \n: Adds a new line to the output.
    • \r: Performs a carriage return.
    • \t: Creates horizontal tab spaces.
    • \v: Creates vertical tab spaces.
    • \NNN: Byte with the octal value of NNN.
    • \xHH: Byte with the hexadecimal value of HH.

Examples of Echo Command

Here are some ways you can use the echo command in Linux:

Changing the Output Format

Using the -e option allows you to use escape characters. These special characters make it easy to customize the output of the echo command.

For instance, using \c let you shorten the output by omitting the part of the string that follows the escape character:

echo -e 'Hello, World! \c This is PNAP!'
Using the echo command with the c escape character  

Note: If you are using the -e option, enter your string enclosed in single quotation marks. This ensures that any escape characters are interpreted correctly.

Use \n any time you want to move the output to a new line:

echo -e 'Hello, \nWorld, \nthis \nis \nPNAP!'
Using the echo command with the n escape character

Add horizontal tab spaces by using \t:

echo -e 'Hello, \tWorld!'
Using the echo command with the t escape character

Use \v to create vertical tab spaces:

echo -e 'Hello, \vWorld, \vthis \vis \vPNAP!'
Using the echo command with the v escape character

Using ANSI escape sequences lets you change the color of the output text:

echo -e '\033[1;37mWHITE'
echo -e '\033[0;30mBLACK'
echo -e '\033[0;31mRED'
echo -e '\033[0;34mBLUE'
echo -e '\033[0;32mGREEN'
Changing output text color

Writing to a File

Use > or >> to include the string in an echo command in a file, instead of displaying it as output:

sudo echo -e 'Hello, World! \nThis is PNAP!' >> test.txt

If the specified text file doesn’t already exist, this command will create it. Use the cat command to display the content of the file:

cat test.txt
Using the echo command to write the output into a file  

Note: Using > overwrites the content of the text file with the new string, while >> adds the new string to the existing content.

Displaying Variable Values

The echo command is also used to display variable values as output. For instance, to display the name of the current user, use:

echo $USER
Using the echo command to display a variable value

Displaying Command Outputs

The echo command allows you to include the result of other commands in the output:

echo "[string] $([command])

Where:

  • [string]: The string you want to include with echo
  • [command]: The command you want to combine with the echo command to display the result.

For instance, list all the files and directories in the Home directory by using:

echo "This is the list of directories and files on this system: $(ls)"
Using the echo command to display the output of other commands

Conclusion

After reading this tutorial, you should know how to use the echo command in Linux.

For more Linux commands, check out our Linux Command Cheat Sheet

Was this article helpful? YesNo   Share onLinkedIn Aleksandar Kovačević With a background in both design and writing, Aleksandar Kovacevic aims to bring a fresh perspective to writing for IT, making complicated concepts easy to understand and approach. Next you should read DevOps and Development SysAdmin Web Servers How to Use the Linux sleep Command with Examples February 3, 2021
The sleep command is used when it is necessary to postpone the execution of commands on the command line or... READ MORE SysAdmin Web Servers How to Use the Linux tee Command January 28, 2021
By default in Linux, a command received through standard input writes directly to standard output... READ MORE Security SysAdmin How To Use The Passwd Command In Linux January 26, 2021
Passwords are the most important feature of security. This article explains and shows examples of how to use... READ MORE SysAdmin Web Servers Linux Commands Cheat Sheet: With Examples February 21, 2020
A list of all the important Linux commands in one place. Find the command you need, whenever you need it or... READ MORE   https://phoenixnap.com/kb/echo-command-linux

标签:Command,string,echo,command,Linux,output,Echo
来源: https://www.cnblogs.com/pengmn/p/16486763.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有