1.11.1 command Substitution
Use the reverse quotation mark "'" (on the key of the wavy line) or $ () in Linux to perform command substitution. Generally more intuitive and more convenient to knock in $ ().
[Email protected] tmp]# echo Can You tell me what's date it is? Oh My Pleasure $ (date +%f)
Can you tell me about date it is? Oh My Pleasure 2016-09-25
[Email protected] tmp]# echo Can You tell me what's date it is? Oh my pleasure ' date +%f ' ? Or use the back quotation marks
Use $ () to allow the command in parentheses to run ahead of the entire command, and then give the entire command as a stdin. It can exist anywhere.
The essence of it is that it opens a child shell through (), runs the command in parentheses in the child shell, runs out of the shell, and then the parent shell packs the result through the $ symbol, because the result is often handed out to external commands, so you can't let the result have a newline behavior. Therefore, the packaging process by default to replace line characters to the space, of course, only one row of results is irrelevant.
The following example shows a multi-line, you can see in the packaging () when the line break is removed, changed to a space, in addition to the last one.
[Email protected] tmp]# (cat abc.sh) | Cat-e
#!/bin/bash$
# file name:abc.sh$
printf "%-s\t%-s\t%s\n" No Name mark$
printf "%-s\t%-s\t%4.2f\n" 1 Sarath 80.34$
printf "%-s\t%-s\t%4.2f\n" 2 James 90.998$
printf "%-s\t%-s\t%4.2f\n" 3 Jeff 77.564$
[[email protected] tmp]# echo $ (cat abc.sh) | Cat-e
#!/bin/bash # filename:abc.sh printf "%-s\t%-s\t%s\n" No Name Mark printf "%-s\t%-s\t%4.2f\n" 1 Sarath 80.34 PR intf "%-s\t%-s\t%4.2f\n" 2 James 90.998 printf "%-s\t%-s\t%4.2f\n" 3 Jeff 77.564$
Use double quotation marks to keep line breaks.
[Email protected] tmp]# echo "$str"
#!/bin/bash
# file name:abc.sh
printf "%-s\t%-s\t%s\n" No Name Mark
printf "%-s\t%-s\t%4.2f\n" 1 Sarath 80.34
printf "%-s\t%-s\t%4.2f\n" 2 James 90.998
printf "%-s\t%-s\t%4.2f\n" 3 Jeff 77.564
The following command does know that the parentheses are open to the child shell.
[[email protected] tmp]# ps-ef|grep bash, (ps-ef|grep bash);p s-ef|grep bash
Root 6557 6554 0 08:41 pts/1 00:00:02-bash
Root 7925 6557 0 15:44 pts/1 00:00:00 grep--color=auto Bash
root 6557 6554 0 08:41 pts/1 00:00:02-bash
root 7926     6557 0 15:44 pts/1 00:00:00-bash Span style= "font-family:; Mso-ascii-font-family: Song body; Mso-hansi-font-family: Song body; Mso-char-type:symbol; Mso-symbol-font-family:wingdings "lang=" en-US "> Sub shell
Root 7928 7926 0 15:44 pts/1 00:00:00 grep--color=auto Bash
Root 6557 6554 0 08:41 pts/1 00:00:02-bash
Root 7930 6557 0 15:44 pts/1 00:00:00 grep--color=auto Bash
1.11.2 about child shells
The child shell inherits the environment variables of the parent shell and some other variables, but some of them are not inherited. After BASH 4, you can use the $BASH _subshell variable to see the number of child shell layers starting from the current process .
1.11.3 when a child shell is produced
1. Run the script
When you run the script with #!/bin/bash in the first line, you use this/bin/bash to open a child shell, and of course other bash. The script finishes running back to the parent shell, so the script does not create any environmental changes to the parent shell.
2. Use parentheses ()
Running a command in () also opens a child shell. So do not randomly write () in Linux to change the priority, unless it is escaped, and the parentheses after the turn to have a space between the contents of the parentheses.
[Email protected] tmp]# echo $BASH _subshell
0
[Email protected] tmp]# (echo $BASH _subshell)
1 ? Description opens a layer of child shells
But take a look at the example below.
[Email protected] tmp]# abc=123
[[Email protected] tmp]# (Echo $abc)
123
The custom variable should not have been inherited, but the result is the result of the parent shell. This is because fork a new process uses the value of the parent process to assign a value. So () the child shell opened by such an operator will get the value of the parent shell, but the script will not be available.
Fork Process: Unix a term for process management is essentially a new process, but instead of loading the code from disk, a copy of the existing process from memory is copied.
3. Use an explicit bash
The actual execution of the script is to run bash into the child shell, which does not use the value of the parent process to assign a value.
[Email protected] tmp]# bash
[Email protected] tmp]# echo $BASH _subshell
0 ? is 0, because now $BASH _subshell the child Shell as the parent Shell of the calculation
Shell Scripting Raiders (Reading notes)--1.11 command substitution and the role of child shells