Tlcl-view the world echo in shell eyes, tlcl-view echo
View the world in shell eyes
Echo-display a line of text
When you enter a command, bash expands the input characters before executing the command.
Expand path
Echo *
Shell expands * to the name of the file in the current directory, and prints the name of the file in the current directory,
Echo d * print all files starting with d in the current directory
Echo * d: print all files ending with d in the current directory.
~ Expand
Echo ~ Print the home directory information of the current user
Echo ~ Test prints the home directory information of the test user.
Arithmetic expression Expansion
Echo $ (expression ))
Echo $ (2*3) + 5 ))
Echo $(2 ** 3) * 5 ))
Note the expression format. arithmetic expressions only support integer, addition, subtraction, multiplication, division, and power operations.
Curly braces
Create multiple text strings from a curly brackets Pattern
Echo hello {1, 2, 3} Will output hello1 hello2 hello3
Echo hello {1, 2, 3} Will output hello 1 2 3
Echo hello {a, B, c} Will output helloa hellob helloc
Note: The elements in {} must be separated by commas (,). They can be characters or strings.
The most common application is to create a series of files or directories.
Mkdir hello {1, 2, 4, 5}
Expand Parameters
Echo $ USER print the USER name
View Variable list
Printenv | less
When the expanded variable does not exist, the system expands it into an empty string.
Echo $ HELLO
Command replacement
Use the output of a command as an extension
Echo $ (ls)
Ls-l $ (which cp) uses the whic cp output information as the ls Parameter
Or
Ls-l 'which cp' old shell support, inverted quotation marks
Reference
Echo hello world
The hello world will be printed on the screen.
Echo the total is $100.00
The total is 00.00 will be printed on the screen
In the first medium, shell will delete unnecessary spaces. In the second medium, $ will expand the variable 1, and 1 is not a variable, so it will be expanded into a null string.
Shell provides a reference mechanism that effectively controls the expansion of characters.
Double quotation marks
The first type of reference controls the expansion of characters.
If you add "" at both ends of the text, all characters except $ \ '(inverted quotation marks) in the text will not have special expanded meanings.
In double quotation marks, parameter expansion, expression expansion, and command expansion are still valid.
Echo "$ USER $(2 ** 3) $ (ls )"
Echo "hello world" will print the hello world
Word Segmentation Mechanism
Echo hello world
The output is hello world.
By default, the word segmentation mechanism searches for spaces, tabs, and line breaks in words and regards them as delimiters between words. Each word is a parameter, in the preceding example, there are two parameters. Each parameter is printed and defined by space.
Similarly, if you enter echo $ (cal)
In this case, a line of data with spaces is printed, without line breaks.
If echo "hello world" is input"
Word Segmentation is not allowed. In fact, "internal content as a parameter exists, but" not added "is two parameters.
Let's see the difference between echo $ (cal) and echo "$ (cal )".
Single quotes
The second type of reference restricts the expansion of characters. single quotation marks limit the expansion of all characters. All characters only have their own meaning and do not have any special meaning.
Escape characters
Use \ to disable the expansion of special characters. Note that \ will be ineffective in single quotes.
Echo \ "hello world \"
Use of backslash escape sequences
\ A sends an alert;
\ B Delete the previous character;
\ C does not end with a line break;
\ F line feed, but the cursor remains at the original position;
\ N wrap and move the cursor to the beginning of the line;
\ R move the cursor to the beginning of the line without line breaks;
\ T Insert tab;
\ V is the same as \ f;
For example, \ a bell
Echo-e '\ a'-e Option to explain escape characters. Although \ does not work in single quotes,-e Option explains it
Echo $ '\'
Cannot directly echo "\"
Finally, I learned a-n option.
-N: output without line breaks
Echo "hello"; echo "world" at this time, the hello world will be output in two rows
Echo-n "hello"; echo "world" will output a line of helloworld