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:
- # 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:
- Jack @ localhost :~ $ Echo $ USER
- Jack
- Jack @ localhost :~ $ Echo $ HOME
- /Home/jack
Example: 2. display the value of a user-defined variable
- Jack @ nextstep4it :~ $ Var1 = 'date'
- Jack @ nextstep4it :~ $ Echo "Today's date time is: $ var1"
- Today's date time is: Mon Jul 28 13:11:37 IST 2014
Example: 3. display a text string
- Jack @ nextstep4it :~ $ Echo "Hi this echo command testing"
- Hithis echo command testing
Example: 4 Use the backslash in the echo command
- 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
- 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
- Jack @ nextstep4it :~ $ Echo-e "Ubuntu \ vis \ vthe \ vbest \ vDesktop \ vOS"
- Ubuntu
- Is
- The
- Best
- Desktop
- 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.
- Jack @ nextstep4it :~ $ Echo-e "\ 033 [31 mMagic of Linux \ 033 [0 m"
- Magic of Linux
The following command prints "Magic of Linux" in bold and red ".
- Nextstep4it @ nextstep4it :~ $ Echo-e "\ 033 [1 m \ 033 [41 mMagic of Linux \ 033 [0 m"
- 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: