Embedded Commands   «Prev  Next»
Lesson 4 When to use embedded execution
Objective Identify when embedded commands are most useful.

When to use Embedded Execution in Unix

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:
    starttime=`date`
    

    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 command's 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:
dayofweek=`date +%A`

Here are some other useful date command options you may want to use.
  • Useful Date Command Options
    The Unix date command displays the date and the time of day. The standard output of the date command looks like this:
    $ date
    Sat Sep 25 14:38:35 PDT 1999
    

    Purpose of the date -d in unix command
    In UNIX-like systems, the date command is used to display or set the system date and time. The -d (or --date) option allows you to specify a different date and/or time string rather than using the current system time. This option is particularly useful for performing various date and time calculations or displaying the date in different formats. The -d option is followed by a date string, which can be an absolute date, a relative date, or a combination of both. The date command then parses this string and outputs the result in the default format or a format specified by the user using the +FORMAT option.
    Here are a few examples of using the date -d option:
    1. Display the date for 7 days in the future:
      date -d '+7 days'
      
    2. Display the date for 2 hours ago:
      date -d '-2 hours'
      
    3. Display the date for a specific date string (e.g., "2023-03-17 12:00:00"):
      date -d '2023-03-17 12:00:00'
      
    4. Display the day of the week for a specific date:
      date -d '2023-03-17' '+%A'
      

    Please note that the -d option is specific to GNU date (commonly found on Linux systems). The BSD date command (found on macOS and some other UNIX systems) uses a different set of options for similar functionality. To achieve the same results on a BSD system, you would use the -j -f options and provide the input format of the date string.
    The date command lets you use options to display pieces of the date and time information in a selected format. If you want to display only the day of the month, you would use this command:
    $ date +%e
    25
    

    Start the format specification with a plus sign. Follow this with one or more percent-sign-marked letters, which indicate which piece of the date you want. The %e represents the day of the month. %b represents the 3-letter abbreviation for the month:
    $ date +%b
    Sep
    

    To display the month and the day of the month, use the command below.
    $ date +%b-%e
    Sep-25
    

    The dash in this command is literal. You can insert characters between the percent-sign-marked pieces of the date and they will be displayed literally in your output: Test out these date command options by logging into your UNIX account and typing them in at the shell prompt.

Next Example of embedded Execution

In this next example, the output of the echo command is clearer than would be the output of running the pwd command by itself.
echo "your current directory is `pwd`"

The output is enhanced by joining two commands, the echo command and the pwd command, in an embedded format, to make the output clearer.
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 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 grep command.

Generating useful file names

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.*
/tmp/psout.Sep-25

Inappropriate usage 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.
$ date
Thu Sep 30 23:47:45 PDT 1999

The following example shows e-e that will not work at all:
$ `ps`
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. Don’t use back quotes around solo commands. Only use them when embedding one command inside another.

SEMrush Software 4 SEMrush Banner 4