--- tags: - bash - shell - scripting - expansion - substitution - text - variable - output - execute - stdout - save - result - return - value --- # Command substitution $( ) ` ` The command substitution expands to the output of commands. These commands are executed in a subshell, and their `stdout` data is what the substitution syntax expands to. All **trailing** newlines are removed (below is an example for a workaround). In later steps, **if not quoted**, the results undergo [word splitting](../../syntax/expansion/wordsplit.md) and [pathname expansion](../../syntax/expansion/globs.md). You have to remember that, because the word splitting will also remove embedded newlines and other `IFS` characters and break the results up into several words. Also you\'ll probably get unexpected pathname matches. **If you need the literal results, quote the command substitution!** The second form `` `COMMAND` `` is more or less obsolete for Bash, since it has some trouble with nesting (\"inner\" backticks need to be escaped) and escaping characters. Use `$(COMMAND)`, it's also POSIX! When you [call an explicit subshell](../../syntax/ccmd/grouping_subshell.md) `(COMMAND)` inside the command substitution `$()`, then take care, this way is **wrong**: $((COMMAND)) Why? because it collides with the syntax for [arithmetic expansion](../../syntax/expansion/arith.md). You need to separate the command substitution from the inner `(COMMAND)`: $( (COMMAND) ) ## Specialities When the inner command is only an input redirection, and nothing else, for example $(