One, command line substitution (a) history substitution
We have studied before!! Represents the execution of the previous command, which is a substitution.
(ii) curly braces replacement
Not very easy to express, directly on the example. For example: We want to create a winc, wind, wine directory for mounting C, D, and E drives.
[email protected]:~$ pwd/home/linux[email protected]:~$ mkdir /home/linux/test/winc /home/linux/test/wind /home/linux/test/wine[email protected]:~$ ls -l /home/linux/testtotal 12drwxr-xr-x 2 linux linux 4096 Jul 20 15:30 wincdrwxr-xr-x 2 linux linux 4096 Jul 20 15:30 winddrwxr-xr-x 2 linux linux 4096 Jul 20 15:30 wine
We used mkdir to create three files at a time, but each file is hard to write an absolute path, you can first enter the directory and then create
[email protected]:~$ cd /home/linux/test[email protected]:~/test$ mkdir winc wind wine
There are actually simpler ways to do this:
[email protected]:~/test$ mkdir win{a,b,c}[email protected]:~/test$ lswina winb winc
This is the curly brace substitution, win{a,b,c} decomposed into: Wina, WINB, Winc. Let's play a more cool one:
mkdir -p user{1,2,3}/{images,music,video}
This is convenient for batch creation of catalogs with common features.
(iii) Substitution of substituted fonts
This we have touched, ~ represents the current user home directory, ~username, which represents the home directory of the specified user.
[email protected]:~$ ls ~Desktop Downloads Music test VirtualBox VMsDocuments exercises Pictures Videos 模板[email protected]:~$ ls ~aliceDesktop Documents Downloads Music Pictures Videos
The first line of the command looks at the current user home directory, and the second line looks at the Alice user's home directory.
(iv) Variable substitution
We've learned this before:
- $ variable Name
- ${variable Name}
(v) Arithmetic substitution
The numbers we normally write in order, +,-, *,/(plus, minus, multiply, divide) are all considered characters, without any mathematical significance. Like what:
[email protected]:~$ a=3[email protected]:~$ b=4[email protected]:~$ echo $a+$b3+4
But you can put them into $ ((...)) , this is the expression of mathematical arithmetic.
[email protected]:~$ a=100[email protected]:~$ b=200[email protected]:~$ echo $((a+b))300
When partitioning, we need to calculate the size of the partition to how many m, such as: you want to calculate the 30G partition equals how many m, you can:
[email protected]:~$ echo $((30*1024))30720
For add, subtract, multiply, except all support, but do not support decimals. It doesn't seem to be a professional math figure. Important thing to say again: Only integer arithmetic is supported, and only integer digits that cannot be divisible in decimals are preserved.
(vi) Command replacement
Directly on the example, through an example to explain the problem:
[email protected]:~/test$ date +%Y%m%d20180720
We found that the date command output is 20180720, and now I want to create a directory where the current time is the directory name.
[email protected]:~/test$ mkdir date +%Y%m%d[email protected]:~/test$ ls -ltotal 8drwxr-xr-x 2 linux linux 4096 Jul 20 18:16 datedrwxr-xr-x 2 linux linux 4096 Jul 20 18:16 +%Y%m%d
Found different from the idea, originally wanted to implement the following command generated by the date of the directory name, but now the shell to the following command as a string.
[email protected]:~/test$ mkdir `date +%Y%m%d`[email protected]:~/test$ ls -ltotal 4drwxr-xr-x 2 linux linux 4096 Jul 20 18:18 20180720
Modify, put the command in the anti-quote, the character in the anti-quote is used as a command, that is, with the command results to replace, found success. There is also a method similar to the arithmetic operation method, but only a layer of parentheses, with $ (command ...) Realize.
[email protected]:~/test$ mkdir $(date +%Y%m%d)[email protected]:~/test$ ls -ltotal 4drwxr-xr-x 2 linux linux 4096 Jul 20 18:29 20180720
Summary: In one line of command, to use the result of another line of command, you need to put that command to:
(vii) path name substitution
We've been exposed to this before.
- * Denotes 0 or more characters
- ? Represents a character
- [...] Represents one of the characters in parentheses
- [^...] Represents a character that is not in parentheses
Two, reference and escape characters
Some characters are used by the shell, such as: semicolons, $, quotes, etc. Bash Shell has three ways to avoid characters being interpreted by the shell:
- Escape
- Double quotes
- Click the number
First we look at escaping:
To create a directory <a>, but we know that < and > are redirection operators that are interpreted as redirected when executed. To keep the shell from being interpreted as a redirect, and just as a normal character, you need to precede the escape. such as: <, so that the shell will not be treated as a redirect operator.
We can also enclose in double quotation marks, except $,!, ' will be interpreted by the shell, while others are treated as ordinary characters:
[email protected]:~/test$ mkdir "<a>"[email protected]:~/test$ mkdir "`date +%Y%m%d`"[email protected]:~/test$ ls -ltotal 8drwxr-xr-x 2 linux linux 4096 Jul 20 19:55 20180720drwxr-xr-x 2 linux linux 4096 Jul 20 19:54 <a>
We will find that putting <a> in double quotes is not interpreted as redirection, but the command in the inverted quotation marks is still interpreted by the shell in double quotes, and a command is replaced.
The last is a single quotation mark, and the characters in the single quotation mark are treated as ordinary characters.
[email protected]:~/test$ mkdir ‘`date +%Y%m%d`‘[email protected]:~/test$ ls -ltotal 4drwxr-xr-x 2 linux linux 4096 Jul 20 19:58 `date +%Y%m%d`
So you need to use single quotes to reference them as they are.
The shell substitution occurs before the command executes. If the parameters of a command have command substitution, be careful, for example:
[email protected]:~/test# find /etc/ -name *.conf | head -2/etc/gnome-vfs-2.0/modules/default-modules.conf/etc/xdg/user-dirs.conf
This command parameter has a *.conf command substitution, but there is nothing in the current test directory, so there is no replacement, just pass the *.conf to the Find command.
[email protected]:~/test# touch a.conf[email protected]:~/test# find /etc/ -name *.conf | head -2
Running again found the same command is different from the result you just ran, which means replacing it first, so what is the replacement result?
[email protected]:~/test# find /etc/ -name a.conf | head -2
First replace the *.conf with the a.conf, because there are a.conf in the current directory that conform to the *.conf substitution rules. As the/etc/did not find the a.conf, so nothing is shown. To avoid this, we should put *.conf in a string to prevent command substitution from occurring. Because "*.conf" is passed to the Find command as a parameter.
Use the Find command to be careful later, it is best to use the "" number of search conditions, it will be more secure.
Let's do an interesting experiment:
Because Lao Mo first understand this detail, so do an experiment to deepen understanding.
[email protected]:~$ cd test[email protected]:~/test$ echo "echo hello" > start[email protected]:~/test$ chmod +x start[email protected]:~/test$ *bash: start: command not found[email protected]:~/test$ PATH=$PATH:$PWD[email protected]:~/test$ *hello
First enter the test directory to do the experiment, do the experiment when we create a new directory, in the inside to do is very safe, to prevent the destruction of the outside directory structure or misoperation caused by the hidden trouble.
After writing the echo Hello string to the start file, it means that the test directory has a file start, and the content in Start is echo hello. Then add execute permissions.
Input * Results Prompt the start command was not found, through the previous study, must be * replaced by the start. We know that the program the shell is going to execute is searched in path, but start does not have a path path, so we're going to add the current directory to path.
By $pwd, add the current directory to the PATH environment variable, then execute *, which shows hello. If you have not learned this knowledge point, think that there is a ghost incident, A * say hello to you ...
Linux Basics-20