Читать книгу Linux Bible - Christopher Negus - Страница 107
Expanding commands
ОглавлениеWith command substitution, you can have the output of a command interpreted by the shell instead of by the command itself. In this way, you can have the standard output of a command become an argument for another command. The two forms of command substitution are $(command)
and `command`
(backticks, not single quotes).
The command in this case can include options, metacharacters, and arguments. The following is an example of using command substitution:
$ vi $(find /home | grep xyzzy)
In this example, the command substitution is done before the vi
command is run. First, the find
command starts at the /home
directory and prints out all of the files and directories below that point in the filesystem. The output is piped to the grep
command, which filters out all files except for those that include the string xyzzy
in the filename. Finally, the vi
command opens all filenames for editing (one at a time) that include xyzzy
. (If you run this and are not familiar with vi
, you can type :q! to exit the file.)
This particular example is useful if you want to edit a file for which you know the name but not the location. As long as the string is uncommon, you can find and open every instance of a filename existing beneath a point you choose in the filesystem. (In other words, don't use grep
from the root filesystem or you'll match and try to edit several thousand files.)