How to Execute Commands within Commands in Bash

In Bash, you can execute commands within commands, also known as nested commands, using backticks (`) or the $() syntax. This allows you to use the output of one command as an argument or input for another. The $() syntax is generally preferred over backticks as it's more readable and supports nesting more easily. This technique is advantageous for:

  1. Avoiding intermediate files: You won't need to create extra files to store the output of one command, making your code more concise and efficient.
  2. Enhancing script readability: Nested commands can often make scripts easier to understand and maintain, as the flow of operations becomes more apparent.
  3. Chaining multiple commands: Nest commands together to perform complex tasks in a single line, streamlining your workflow.

Using Backticks (`)

result=`command1` echo "Result of command1: $result" # Using the result of command1 in command2 result2=`command2 $result` echo "Result of command2: $result2"

In this example, the output of command1 is stored in the variable result, and then command2 is executed with result as an argument.

Using $() Syntax

result=$(command1) echo "Result of command1: $result" # Using the result of command1 in command2 result2=$(command2 $result) echo "Result of command2: $result2"

Here, the $() syntax achieves the same result as backticks but is considered more modern and readable.

Nested Commands

You can also nest commands within commands to create more complex operations. For example:

result=$(command1 "$(command2)") echo "Result of command1 with command2 output: $result"

In this case, the output of command2 is used as an argument for command1.

Example with Pipeline:
result=$(echo "Hello" | tr '[:lower:]' '[:upper:]') echo "Uppercase Result: $result"

In this example, the output of echo "Hello" is passed through a pipeline to tr (translate) command, which converts the text to uppercase using the character set specified.

Complex Example:
result=$(grep "pattern" "$(cat file.txt)") echo "Result of grep: $result"

Here, cat file.txt is executed first, and its output is passed as an argument to the grep command. The grep command then searches for the specified pattern in the contents of the file.

Best Practices

Favor $()

Use the $() syntax for improved code readability. It's a modern and clearer alternative to backticks for executing commands within commands in Bash, making your code more maintainable and easy to understand.

Quoting is Beneficial

When nesting commands, quote the output of the inner command within the outer command to prevent unexpected issues with spaces or special characters. This ensures accurate parsing and execution of the nested command.

Consider Error Handling

Be cautious about exit codes when using nested commands, as they can complicate error handling. Check the success or failure of each command within the nesting to handle errors appropriately and make your script robust.

Descriptive Variable Names

Use clear and descriptive variable names for intermediate results when storing the output of nested commands. This enhances script understanding, making it easier for you and others to comprehend the purpose of each step in the code.


To execute commands within commands in Bash, use either the $() syntax or backticks. Choose $() for better readability, quote nested command outputs to handle spaces or special characters, consider error handling with exit codes, and use descriptive variable names for intermediate results to enhance script understanding.