Читать книгу Linux Bible - Christopher Negus - Страница 108
Expanding arithmetic expressions
ОглавлениеSometimes, you want to pass arithmetic results to a command. There are two forms that you can use to expand an arithmetic expression and pass it to the shell: $
[expression] or $
(expression). The following is an example:
$ echo "I am $[2020 - 1957] years old." I am 63 years old.
The shell interprets the arithmetic expression first (2020 - 1957
) and then passes that information to the echo
command. The echo
command displays the text with the results of the arithmetic (63
) inserted.
Here's an example of the other form:
$ echo "There are $(ls | wc -w) files in this directory." There are 14 files in this directory.
This lists the contents of the current directory (ls
) and runs the word count command to count the number of files found (wc -w
). The resulting number (14, in this case) is echoed back with the rest of the sentence shown.