Embedded Commands   «Prev  Next»
Lesson 6

Embedded command execution Conclusion

In this module, you learned to assign the output of a command to a variable. You also learned the difference between embedded command execution and output redirection. We looked at some useful and non-useful ways to use embedded command execution, and explored a complex command containing an embedded command.

Assign the output of a command to a variable using Unix Shell

In Unix-like shells, you can assign the output of a command to a variable using command substitution. Command substitution allows you to capture the output of a command and store it in a variable for later use. There are two syntaxes for command substitution in Unix-like systems:
  1. Using $(command) syntax (preferred):
  2. Using backticks (command) syntax:

    Here's an example of assigning the output of a command to a variable:
    # Using $(command) syntax
    echo "Today's date is: $current_date"
    # Using backticks syntax
    echo "Today's date is: $current_date"

    In both examples, the date command is executed, and its output is captured and stored in the current_date variable. The echo command then prints the string "Today's date is: " followed by the stored date.
    Please note that the $(command) syntax is generally preferred over backticks because it is more readable and can be easily nested.

Key commands

This module reviewed the following UNIX commands:
  1. date
  2. hostname
  3. wc -l

Terms and concepts

This module introduced you to:
  1. embedded command execution
  2. Pipes
  3. Cat
  4. Environment variable

How are pipes used in Unix shell programming?

In Unix shell programming, pipes (|) are used to connect the output of one command to the input of another command, allowing you to chain multiple commands together and perform complex operations. This is known as "piping."
When you use a pipe, the standard output (stdout) of the command to the left of the pipe is connected to the standard input (stdin) of the command to the right of the pipe. This allows data to flow seamlessly between commands without the need for temporary files or variables.
Here is an example of using pipes in Unix shell programming:
cat file.txt | grep "example" | wc -l

In this example, we have three commands connected by pipes:
  1. cat file.txt: The cat command reads the contents of file.txt and sends it to stdout.
  2. grep "example": The grep command reads from stdin (connected to the output of the previous cat command), searches for lines containing the string "example", and sends the matching lines to stdout.
  3. wc -l: The wc -l command reads from stdin (connected to the output of the previous grep command) and counts the number of lines, printing the result to stdout.

The final result of this pipeline is the count of lines in file.txt containing the string "example". Pipes are a powerful feature in Unix shell programming, enabling you to create complex data processing pipelines with a combination of simple commands.

In the next module, a feature of the shell called job control will be discussed.
You can use job control to manipulate processes and to push longer tasks into the background from your interactive shell or from a script. You will also learn how to start commands that will continue to run after you logout.

Embedded Syntax - Quiz

Click the quiz link below to test your knowledge of embedded command syntax and features.
Embedded Syntaz - Quiz