Embedded Commands   «Prev  Next»
Lesson 2 Defining embedded execution
Objective Explain how embedded command execution works.

Defining embedded Execution in Unix

The shell will perform embedded execution on any section of a command that is surrounded by back quotes. Embedded execution uses the output of the embedded command as part of the text of a surrounding command. Here is an example command using embedded execution:
The back quoted command, the ls is run first and the output is assigned to the var1 variable.

var1=`ls`

How does embedded command execution work in Unix?

In Unix-like systems, embedded command execution, also known as command substitution, allows you to execute a command within another command by substituting the output of the embedded command as an argument for the outer command. This technique is useful when you want to use the output of one command as input for another command.
There are two ways to perform command substitution in Unix-like systems:
  1. Using $(command) syntax:
    echo "Today is $(date +%A)"
    

    In this example, the date +%A command is executed, and its output (the current day of the week) is substituted in place of $(date +%A). The echo command then prints the string "Today is " followed by the day of the week.
  2. Using backticks (command) syntax:
    echo "Today is `date +%A`"
    

    This example is equivalent to the previous one but uses backticks instead of $(command) syntax. The result is the same: the echo command prints the string "Today is " followed by the day of the week.
  3. While both syntaxes can be used for command substitution, the $(command) syntax is generally preferred because it is more readable and can be easily nested:
    echo "The current time in New York is 
    $(date -u -d "$(TZ=America/New_York date +%Z%z)" +"%Y-%m-%d %H:%M:%S")"
    

    In this example, we use nested command substitution to calculate and display the current time in New York. The inner command substitution sets the timezone to "America/New_York" and outputs the offset, which is then used by the outer command substitution to display the correct time.

The Difference between embedded execution and Redirection

Notice that this differs from redirection, where the output of one command is used as the input to another command. Redirection can be used to capture command output in a file, as in:
Embedded execution (command substitution) and redirection are two different concepts in Unix-like systems, serving distinct purposes:
  1. Embedded execution (command substitution):
    Embedded execution, or command substitution, is a technique used to execute a command within another command, using the output of the embedded command as an argument for the outer command. Command substitution is typically used when you want to use the result of one command as input for another command or as part of a string.
    1. $(command): The more modern and preferred syntax.
    2. command: The older syntax, using backticks.

    Example:
    echo "Today is $(date +%A)"
    

    In this example, the date +%A command is executed, and its output (the current day of the week) is substituted in place of $(date +%A). The echo command then prints the string "Today is " followed by the day of the week.
  2. Redirection:
    Redirection is a technique used to change the flow of standard input (stdin), standard output (stdout), or standard error (stderr) streams of a command. Redirection allows you to read input from a file, write output to a file, or pipe the output of one command as input to another command. Redirection is useful for processing data, managing logs, or chaining multiple commands together to perform complex operations.
    There are various symbols for different types of redirection:
    1. >: Redirects stdout to a file, overwriting the file if it exists.
    2. >>: Redirects stdout to a file, appending to the file if it exists.
    3. <: Redirects stdin from a file.
    4. 2>: Redirects stderr to a file, overwriting the file if it exists.
    5. 2>>: Redirects stderr to a file, appending to the file if it exists.
    6. |: Pipes stdout of one command as stdin to another command (also known as "piping").

    Example:
    grep "example" input.txt > output.txt
    

In this example, the grep command searches for the string "example" in the input.txt file. The > redirection symbol directs the output (stdout) of the grep command to the output.txt file, overwriting the file if it exists.
In summary, embedded execution (command substitution) is used to execute a command within another command and use its output as an argument, while redirection is used to change the flow of input/output/error streams between commands and files.


ls > file1

Two-step process of embedded execution

The ls command is run first and the output is assigned to the file named file1. Notice that with redirection, your command is processed once. With embedded command execution, your command goes through a two-step process.