Echo command instance

Source: Internet
Author: User

Echo command instance

The echo command is a built-in shell command used to display the value of a variable or print a line of text.

The echo command plays an important role in building shell scripts.

Syntax:
  1. # Echo [Options] [String]

The items in square brackets are optional. A string can be defined as a finite sequence of characters (such as letters, numbers, symbols, and punctuation marks ).

When the echo command is used without any options or strings, it returns a blank line on the display screen, followed by another line to keep up with the command prompt. This is because pressing the Enter key means sending a signal to the system to start a new line, and echo repeats the signal.

Option:
  • -N: Do not output new lines.
  • -E enable backslash escape Interpretation
  • -E disable backslash escape interpretation (default)

If you use the-e option, you can use the following escape sequence:

  • \ Backslash
  • \ A warning (BEL)
  • \ B backslash
  • \ C does not generate further output
  • \ E escape
  • \ F form feed
  • \ N New Line
  • \ R newline character
  • \ T horizontal tabulation
  • \ V vertical tabulation
  • Byte NNN (1 to 3 digits) represented by \ 0NNN octal values)
  • \ XHH hexadecimal value indicates the byte NNN (1 to 2 digits)
10 Linux Interview Questions and Answers in different application scenarios

10 core Linux Interview Questions and answers

Example: 1. display the value of the system-defined variable

Using the set command, we can list system-Defined variables. To print the values of these variables, we can use the echo command:

  1. Jack @ localhost :~ $ Echo $ USER
  2. Jack
  3. Jack @ localhost :~ $ Echo $ HOME
  4. /Home/jack
Example: 2. display the value of a user-defined variable
  1. Jack @ nextstep4it :~ $ Var1 = 'date'
  2. Jack @ nextstep4it :~ $ Echo "Today's date time is: $ var1"
  3. Today's date time is: Mon Jul 28 13:11:37 IST 2014
Example: 3. display a text string
  1. Jack @ nextstep4it :~ $ Echo "Hi this echo command testing"
  2. Hithis echo command testing
Example: 4 Use the backslash in the echo command
  1. Jack @ nextstep4it :~ $ Echo-e "Ubuntu \ bis \ bthe \ bbest \ bDesktop \ bOS"

The above command will print:

Ubuntuisthebest1_topos

Example: 5 use a tab space in the echo command
  1. Nextstep4it @ nextstep4it :~ $ Echo-e "Ubuntu \ tis \ tthe \ tbest \ tDesktop \ tOS"

The above command will display the following output:

Ubuntu is the best Desktop OS

Example: 6 Use vertical tabs in the echo command
  1. Jack @ nextstep4it :~ $ Echo-e "Ubuntu \ vis \ vthe \ vbest \ vDesktop \ vOS"
  2. Ubuntu
  3. Is
  4. The
  5. Best
  6. Desktop
  7. OS
Example: 7. Set the color output of the echo command

The echo command can modify the font type, background color, and font color. The escape sequence \ 033 can be used to change the font attributes. To make the escape sequence take effect, you must use the-e option. Some escape codes are listed below:

  • [0 m: normal
  • [1 m: bold
  • [4 m: The font is underlined
  • [7 m: reverse foreground and background color
  • [8 m: invisible characters
  • [9 m: cross-line font
  • [30 m: Gray
  • [31 m: red
  • [32 m: green font
  • [33 m: Brown
  • [34 m: Blue font
  • [35 m: Purple font
  • [36 m: light blue font
  • [37 m: light gray font
  • [38 m: Black font
  • [40 m: black background
  • [41 m: red background
  • [42 m: green background
  • [43 m: brown background
  • [44 m: blue background
  • [45 m: purple background
  • [46 m: light blue background
  • [47 m: light gray background

The following command prints the output in red.

  1. Jack @ nextstep4it :~ $ Echo-e "\ 033 [31 mMagic of Linux \ 033 [0 m"
  2. Magic of Linux

The following command prints "Magic of Linux" in bold and red ".

  1. Nextstep4it @ nextstep4it :~ $ Echo-e "\ 033 [1 m \ 033 [41 mMagic of Linux \ 033 [0 m"
  2. Magic of Linux

Via: http://www.nextstep4it.com/categories/unix-command/echo-command/

Translator: GOLinux Proofreader: wxy

This article was originally translated by LCTT and launched with the Linux honor in China

This article permanently updates the link address:

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.