LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Why necessarily " " ? (https://www.linuxquestions.org/questions/programming-9/why-necessarily-482994/)

BiThian 09-13-2006 02:56 AM

Why necessarily " " ?
 
I was looking for the lines that begin with MANPATH (not MANPATH_MAP!) in the file man.conf.When I entered:
Code:

grep ^\<MANPATH\> /usr/lib/man.conf
, I didn't get any output.But when I tried:
Code:

grep "^\<MANPATH\>" /usr/lib/man.conf
it worked. What was the problem with my first snippet code?
Thanks!

zhangmaike 09-13-2006 03:23 AM

The shell will interpret special characters unless you escape them (with \) or enclose them in quotes.

For example, the command:
Code:

echo *
Will print the contents of the current directory, because the shell will evaluate the * and replace it with each file in the current directory (seperated by white space, of course).

While the command:
Code:

echo "*"
Simply prints *

You should look at the bash manpage. There is a section about this called "quoting".

BiThian 09-13-2006 03:44 AM

Yes, I know the quoting problem, but this is what I don't understand:
Code:

grep ^MANPATH /usr/lib/man.conf
-I get output-

Code:

grep "^MANPATH" /usr/lib/man.conf
-I get the same output-

Why doesn't happen the same when I enter the commands from my first post?

zhangmaike 09-13-2006 03:50 AM

If you try:
Code:

echo ^MANPATH
The output is:
Code:

^MANPATH
The ^ character has no special meaning.

In your first post, you were using \>

The > is a special character (used with pipes). The shell will see the \ and assume that you meant to escape the > character, not that you're using grep regular expression syntax.

See for yourself using the arguments for grep from your first post. Try:
Code:

echo ^\<MANPATH\>
(the output is ^<MANPATH>)

Code:

echo "^\<MANPATH\>"
(the output is ^\<MANPATH\>)

BiThian 09-13-2006 03:53 AM

What can I say...A big "Thanks"!

zhangmaike 09-13-2006 03:56 AM

you\'re\ welcome
:)


All times are GMT -5. The time now is 08:38 PM.