Lesson 6.7: Command substitution
$(command): putting a command's output inside another command or a variable; quoting the result; the old backtick form; arithmetic with $(( )); typical uses such as dated file names and counting.
13 min
You will learn to
- Use $(command) inside another command
- Store command output in a variable
- Build dated file names with $(date +%F)
- Calculate with $(( ))
- Explain why the result should usually be quoted
Key terms
- Command substitution $( )
- The shell runs the command inside $( ), then replaces the whole $( ) with its output (trailing newlines removed).
- Backticks ` `
- The older syntax for the same thing. Harder to read and to nest.
- Word splitting of the result
- Unquoted, the output is split into separate arguments at spaces and newlines; inside double quotes it stays one argument.
- Arithmetic expansion $(( ))
- Calculates integer expressions: + - * / % and parentheses; variable names can be used without $.
← Command history · All lessons · Searching text with grep →