Embedded Commands   «Prev  Next»
Lesson 3 Embedded command syntax
Objective Form correct embedded command statements.

Unix Embedded Command Syntax

Forming correct embedded command statements entails using back quotes correctly with other elements of the command syntax.

Using backquotes

First you must surround the embedded command with back quotes.
Take a minute to locate the back-quote key on your keyboard. You will often find the back quote on the same key as the tilde (~) character. The position of this key changes from keyboard to keyboard. Do not confuse the back-quote character with the single-quote character. The single-quote character is on the same key as the double-quote character.

Using other quotes

When you are using back quotes, and other quotes are necessary in your statement, do not use single quotes; use double quotes or no quotes at all. The following command, which uses double quotes, will work properly:

$ echo "You are currently in the `pwd ` directory"
You are currently in the /tmp directory

The embedded pwd command returns the name of your current working directory, which is then included in the echo command's output.
The same command, this time using single quotes, will not work properly:

$ echo ‘You are currently in the `pwd` directory’
You are currently in the `pwd` directory

Single quotes do not allow you to use embedded commands. They preserve the back-quotes exactly as seen and do not interpret them as a symbol to perform embedded command execution. If you do not want the shell to perform embedded execution on back quotes you can surround them in single quotes.

Using pipes

  1. can use pipes inside embedded commands

$ echo "There are `cat fullnames | wc –l` lines in 
your file"
There are 5 lines in your file

The embedded command is always executed first. It counts the number of lines in the fullnames file. In the above example, the output of the command cat fullnames | wc –l is 5. This output is inserted into the surrounding echo command to produce the output you see.

Unix wc Command

$ cat letter
Dear Mom,
Final exams are coming up next week.
Please send food and money.
Love,
Susan

Run the wc command on this file:
$ wc letter
       8      16      90 letter

The wc command tells you that this file has 8 line, 16 words, and 90 characters. You can display just the number of lines using the –l option, the number of words by using the –w option, or the number of characters by using the –c option. Here is the same command using the –l option:
This limits the output to just the number of lines in the letter file.

$ wc –l letter
8 letter

Carriage returns

The shell retains carriage returns in your embedded command’s output when you use double quotes in the command surrounding your embedded command. The shell translates carriage returns into spaces if you omit the double quotes. For example, assume you have a file called fullnames with the following contents:

$ cat fullnames
Harry Smith
Fred Brown
Sally Green
Doris Jones
Martha Jackson

If you cat this file in an embedded command surrounded by double quotes, the carriage returns will be retained in the output. The process of displaying a file’s contents with the UNIX cat command is called “cat”ing the file.

$ echo "The names 'cat fullnames' are in your file"
The names Harry Smith
Fred Brown
Sally Green
Doris Jones
Martha Jackson are in your file

If you run the same command omitting the double quotes the carriage returns will be translated into spaces.
$ echo The names `cat fullnames` are in your file
The names Harry Smith Fred Brown Sally Green Doris 
Jones Martha Jackson are in your file

Embedded Command - Exercise

Click the exercise link below to practice forming correct embedded statements
Embedded Command - Exercise