The terminal is the window where the user interacts with the shell environment, and most of the interaction results are displayed directly to the user from the terminal, so this is the basis for the friendly display results.
1. Overview of Commands
ECHO is the basic Terminal output command that directly enters the input parameters and the command format is as follows:
echo [options] toBeOutput
Detailed description is as follows:
2. Description of characteristics and examples1. Line break
By default, a line break is added after each call. Use the-n option to eliminate this default value.
[Email protected]:~$ echo test a line
[Email protected]:~$ test a line
[Email protected]:~$ echo-n test a line
Test a line [email protected]:~$
2. Quotation marks
Echo's parameters can be output in three different ways, using double quotes, single quotes, and no quotes. Each of these methods has its own characteristics:
- Using double quotes, you can parse the values of the internal variables, but the interior cannot contain "! ", this symbol has a special meaning, you can use the"! " Escape, or use set +h before Echo
- With single quotes, all content is printed as characters, that is, the values of the internal variables are not parsed, and can contain "! ”
- When no quotation marks are used, there is no delimiter in the shell and therefore cannot be used in the text. Although you can output "! ", but if the internal contains"; ", it will be considered by the shell as multiple commands, resulting in an error.
[Email protected]:~$ Var=world
[Email protected]:~$ echo Welcome to Shell, $var
[Email protected]:~$ Welcome to Shell, world
[Email protected]:~$ echo "Welcome to Shell, $var"
[Email protected]:~$ Welcome to Shell, world
[Email protected]:~$ echo ' Welcome to Shell, $var '
Welcome to Shell $var
[Email protected]:~$ echo welcome; Hello World
Welcome
Hello:command not found
[Email protected]:~$ echo "Welcome!hello world."
BASH:!hello:event not found
[Email protected]:~$ echo "Welcome!hello world."
Welcome!hello World.
[Email protected]:~$ echo ' Welcome!hello world '
Welcome!hello World.
[Email protected]:~$ echo Welcome!hello world.
Welcome!hello World.
3. Escaping
Echo's-e option supports escaping strings within double quotes:
echo-e"包含转义序列的字符串"
Escape characters The above details list all the transfer character formatting.
Use the-e option to explicitly de-escape and output all characters as-is. The default option is to not use escape characters.
[Email protected]:~$ echo-e "\ T"
[Email protected]:~$ echo "\ T"
\ t
[Email protected]:~$ echo-e "\ T"
\ t
4. Color output
The escape sequence implements the color. Text color: reset = 0, black = 30, red = 31, green = 32, yellow = 33, blue = 34, magenta =35, cyan = 36, white = 37.
Background color: reset = 0, black = 40, red = 41, green = 42, yellow = 43, blue = 44, magenta =45, cyan = 46, white = 47.
[Email protected]:~$ echo-e "\e[1:42m Green background \e[0m"
5. Expansion
printf can also be used for terminal output, using parameters and formats similar to those in C. You can specify a format string, specify the width of the string, align left and right, and so on. Line breaks are not added by default.
[Email protected]:~$ printf "%-5s%-10s%-4.2f\n" Num James 80.324
Num James 80.32
"-" represents left-aligned and is aligned to the right by default. The "4.2" represents a 4-character width and retains two decimal places. Placeholders such as "s/f/c" represent types.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Linux Command Memo Instance (1)--terminal output