Embedded Commands   «Prev  Next»
Lesson 1

Embedded Command Execution in Unix Shell

This module introduces you to the embedded command execution feature of the UNIX shells.
It takes the output of the embedded command and uses it as part of the surrounding command. This allows you to capture command output in shell variables or create a command on the fly based on information from another command.
Question: Are the "embedded command execution" feature of the UNIX shell and "command substitution" the same thing?
No, the "embedded command execution" feature of the UNIX shell and "command substitution" are not the same thing, although they are related concepts.
Command substitution is a feature of the UNIX shell that allows the output of a command to be used as an argument for another command. This is done by enclosing the command within backticks or using the $( ) syntax. For example, the command echo $(date) would substitute the output of the date command into the echo command, resulting in the current date being displayed.
Embedded command execution, on the other hand, allows commands to be executed within a larger command or script. This is done by enclosing the command within backticks or using the $( ) syntax, similar to command substitution. However, in embedded command execution, the output of the command is not used as an argument for another command, but rather is incorporated directly into the larger command or script.
For example, in the command echo "The current directory is pwd", the pwd command is executed within the echo command, and the output of the pwd command (the current directory) is embedded within the larger echo command.
While these features are similar in syntax and usage, they serve different purposes. Command substitution is used to capture the output of a command and use it as an argument for another command, while embedded command execution is used to execute commands within a larger command or script.

By the end of this module you will be able to:
  1. Explain how embedded command execution works
  2. Identify when embedded commands are most useful
  3. Correctly form an embedded command
  4. Embed a command within a for loop
  5. Embed a command within other common commands

Embedded let ( ((..)) )

The let command returns a status code of 1 if an expression is zero, or 0 if the expression is a value other than zero. In the same way that the test command can be expressed with a pair of square brackets for easier reading in compound statements, the let command also has a form for easier reading: double parentheses. The for loop, as found in other programming languages, is created using an embedded let, as shown in Listing 3.1.

#!/bin/bash
# forloop.sh: Count from 1 to 9
for (( COUNTER=1; COUNTER < 4; COUNTER++ )) ; do
printf "The counter is now %d\n" "$COUNTER"
done
exit 0

The first expression in the double parentheses is executed when the loop is started. Each time the loop is restarted, the third expression is executed and the second expression is checked.When the second expression returns false, the loop ends.
$ bash forloop.sh
The counter is now 1
The counter is now 2
The counter is now 3

Example of command substitution

Var='date', Var=$(date)

Another very common way of setting a variable is to set its value to the output of a given command. This is really a variant of the fi rst format:
VAR=value. 
If you want a variable set to "Monday" on Mondays, "Tuesday" on Tuesdays, and so on, you can use the %A fl ag to the date command, which tells date to give you the appropriate word for today, in the current locale.

command substitution is the act of inserting the output of one command as another command. There are two forms of command substitution.
  1. The standard form uses backticks around the command line to indicate command substitution;
  2. The newer form uses $(cmd).
Command substitutions can be nested; backticks have to be escaped with a backslash. The following code snippets show the two forms; the quotes around the variable are required to keep the linebreaks between the output lines. The final linebreak is always removed.

$ foo='ls -l \'which grep\' /usr/bin/test'
$ echo FOO is: "$foo"
FOO is: -rwxr-xr-x 1 root root 119288 Apr 22 2010 /bin/grep
-rwxr-xr-x 1 root root 30136 Apr 28 2010 /usr/bin/test
$ bar=$(ls -l $(which grep) /usr/bin/test)
$ echo BAR is: "$bar"
BAR is: -rwxr-xr-x 1 root root 119288 Apr 22 2010 /bin/grep
-rwxr-xr-x 1 root root 30136 Apr 28 2010 /usr/bin/test
$

Unix Shell Programming