- [Instructor] The bash shell has characters
called meta-characters that have special meaning.
For example, less than and greater than symbols,
caret, dollar sign, period, asterisk, question mark,
square brackets, pipe, curly brackets, parentheses,
plus and backslash, single quotes and double quotes.
We talked about most of these before, in Chapter One.
Now we should talk about escaping characters and quoting.
If we want to get the value of a variable,
we can use the Echo command
and prefix the variable with a dollar sign.
This tells Echo to read the value of the variable.
In a terminal type in echo, space,
dollar sign, capital U-S-E-R and hit Enter.
This shows us the value of the user variable.
We can also include this variable in a string of characters.
Type in echo, space, My name is,
dollar sign, capital U-S-E-R and hit Enter.
The result is, on my computer, is My name is Grant.
Now let's place a string inside double quotes.
Use your up arrow and place double quotes
around My name is, dollar sign USER and hit Enter.
The result is the same.
However, changing double quotes
to single quotes gets us something different.
Use your up arrow again and change
the double quotes to single quotes.
It should now read echo, space, single quote,
My, space, name, space, is, space,
dollar sign, uppercase USER, single quote and hit Enter.
The result is My name is, dollar sign, user.
Single quotes tell the shell
to present the characters inside literally.
Special characters such as the dollar sign
have no meaning inside single quotes.
We use single quotes when we want to either present
the characters literally or pass the characters
to a command unaltered by the shell.
For instance, to pass a search criteria to the Grep command
that includes an asterisk, we'd use single quotes.
Type in grep, space, single quote,
dot, asterisk, slash, U-D-P,
single quote, space, slash, E-T-C, slash, services.
This line searches the Etsy services file for lines
that start with anything and include slash U-D-P.
Normally the dot, asterisk
and slash characters have special meaning.
Here they get used literally
because of the single quotes.
Hit Enter.
If we only want to escape one character,
we can use the backslash.
For instance, to Echo the contents
of the dollar sign user variable again,
type in echo, space, dollar sign,
capital U-S-E-R, and hit enter.
In this case, the dollar sign gets interpreted by bash
as a meta-character and treats USER as a variable.
If we want to Echo the characters literally
we could just escape the dollar sign
so it has no special meaning.
Type in echo, space, backslash,
dollar sign, capital U-S-E-R and hit Enter.
You can see in this case, it echoed out.
Dollar sign U-S-E-R, literally.
There are many times when you may need
to escape the escape character.
For instance, if you wanted
to display the backslash character on the screen,
you would have to surround it by single quotes.
Type in echo, space, single quote,
backslash, single quote and hit enter.
By surrounding the backslash
with single quotes, we escaped it.
We can also do the opposite for single quotes.
Type in echo, space, backslash,
single quote, text, backslash,
single quote and hit enter.
You can see that the result is
single quote text single quote.
We used backslashes to escape the single quotes.