When to use embedded execution
Identify when embedded commands are most useful.
This lesson introduces you to the situations in your shell scripts where embedding commands are most useful. We will also look at examples
where embedded commands are not useful. Shell programmers sometimes overuse this feature at first, resulting in commands that do not work at
all or commands that force the shell to do extra work.
Saving command output to a variable
Embedded command execution is used to save the output of a command to a variable. This is especially useful when the command has output that
will change frequently, as do the date or pwd commands. To save the starting time of a script to a variable you might run this command:
The UNIX date command outputs the current date and time. The assignment statement above will copy the output of the date command to the
variable start time. The date commands output is constantly changing, and putting this statement at the beginning of the script allows
you to capture the exact starting time of the script every time you execute it.
Enhancing the output of a command
Embedded command execution can also be used to enhance the output of a command, by making it clearer.
In the following example, the code makes it clear that date +%A returns the day of the week:
Here are some other useful date command options you may want to use.
Useful Date Command Options
In this next example, the output of the echo command is clearer than would be the output of running the
pwd command by itself.
The output is enhanced by joining two commands, the echo command and the
echo "your current directory is `pwd`"
pwd command, in an embedded format, to make the output
Using output as part of another command
Embedded command execution is useful when the output of one command must be used as part of another command. In this example, we would like to
search the /etc/hosts file for the current host.
grep `hostname` /etc/hosts
The example above uses the UNIX
Generating useful file names
grep command, which searches a file for a string and prints out any lines containing the string. We
want to search the /etc/hosts file for the current host name, so we embed the hostname command, which outputs the name of the host you are
logged in to. In this command, the output of the hostname command is used as part of the surrounding
You might also use this feature to generate a useful file name. The following command captures the output of the
ps ef command
to a file whose name contains the current day and month. This information is generated using the
date +%b-%e command:
$ ps –ef > /tmp/psout.`date +%b-%e`
$ ls /tmp/psout.*
The command below shows an embedded execution that is not useful:
$ echo `date`
Thu Sep 30 23:47:45 PDT 1999
This command echoes the output of the date command to the screen. However, this command is extra work for the shell.
It runs the date command to find the date and time, then it echoes this information to the screen. This is not necessary, and the shell must
go through the 2-step embedded command execution process to run the command. You can run the date command by itself instead.
The following example shows e-e that will not work at all:
Thu Sep 30 23:47:45 PDT 1999
ksh: PID: not found
Look at the error message from this command. Because back quotes are used around a solo command, instead of an embedded command, the shell is
trying to take the first word of the command output and run it as a command. Dont use back quotes around solo commands. Only use them
when embedding one command inside another.