Unix Embedded Command Syntax
Forming correct embedded command statements entails using back quotes correctly with other elements of the command syntax.
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
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.
- can use pipes inside embedded commands
$ echo "There are `cat fullnames | wc –l` lines in
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.
The shell retains carriage returns in your embedded commands 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
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 files contents with the UNIX cat command is called cating the file.
$ echo "The names 'cat fullnames' are in your file"
The names Harry Smith
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