Alternatively shell commands and command combinations

Backticks Linux, "` "(the wavy line on the key) or $ () command to execute the replacement. Use parentheses () to combine a series of commands.

[root@localhost ~]# echo what date it is? $(date +%F)
what date it is? 2019-12-07
[root@localhost ~]# echo what date it is? `date +%F` 
what date it is? 2019-12-07

Note: The backtick and $ () is almost equivalent to the basic, but try to use the $ () . Anti quotation marks at two inconveniences: (1) when a command or alternatively comprise nested quotes, Backspace very troublesome, as $ () to read. (2) Anti escaping rules to quote the backslash is less clear, but the $ () will escape the backslash in the normal way. See http://mywiki.wooledge.org/BashFAQ/082

Use $ () command allows brackets advance to run the entire command, and then insert the results of the command replaces the symbol. Because often the result of command substitution to an external command, the results should not have let the line break, so by default will replace all line breaks for space (virtually all white space are compressed into a single space).

E.g:

[root@localhost ~]# echo -e "a\nb"
a
b
[root@localhost ~]# echo `echo -e "a\nb\t   \tc"`
a b c
[root@localhost ~]# 

Double quotes can be left blank character.

[root@localhost ~]# echo "`echo -e "a\nb\t   \tc"`"
a
b               c

Probably be understood from the above, the command is divided into two alternative processes: (1) opener executes commands shell (2) and outputs the result sub-shell package insert on the command line. But packed output process can be controlled (e.g. using the above double quotation marks).

So, if you want to replace the results of multi-line commands get stored in the variable (variable holds multiple rows of data). It may operate as follows:

[root@localhost ~]# var="`echo -e "a b\n1 2"`"
[root@localhost ~]# echo $var
a b 1 2

In many cases, the need to use the command line "cat a.txt | command" or perform $ (cat a.txt) to deliver content files a.txt, but this is not the best approach. They are equivalent to more efficient methods are the "<a.txt" and "$ (<a.txt)".

If you use parentheses to surround a series of commands, these commands can be made independent of the current environment running bash. This is actually a command group.

E.g:

[root@localhost ~]# (umask 077;touch new.txt;ls -l new.txt)
-rw-------. 1 root root 0 12月  7 21:56 new.txt

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Guess you like

Origin www.cnblogs.com/liujunjun/p/12003592.html