Home » Infrastructure » Unix » How to grep $
How to grep $ [message #441255] Fri, 29 January 2010 20:05 Go to next message
Messages: 83
Registered: August 2008
I tried to grep the word include $ like "MGMT$DB" , but get the following error in Linux.

$ grep  "MGMT[\$]DB" test1
Variable name must contain alphanumeric characters.
$ grep  "MGMT$DB" test1
DB: Undefined variable.
$ grep  "MGMT\$DB" test1
DB: Undefined variable.
grep "MGMT[$]DB" test1
Illegal variable name.

What is the correct pattern to grep $?


[Updated on: Fri, 29 January 2010 20:08]

Report message to a moderator

Re: How to grep $ [message #441273 is a reply to message #441255] Sat, 30 January 2010 01:39 Go to previous messageGo to next message
Michel Cadot
Messages: 58583
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator
Depending on your shell:
grep  'MGMT$DB' test1
grep  'MGMT\$DB' test1


[Updated on: Sat, 30 January 2010 01:39]

Report message to a moderator

Re: How to grep for string with $/Dollar included. [message #441547 is a reply to message #441255] Mon, 01 February 2010 19:51 Go to previous message
Messages: 5
Registered: April 2008
Location: Atlanta and/or Kansas Cit...
Junior Member
Your posted question says that you were having problems using a unix "grep" to search for text with a dollar ($) included.

In this case, you wanted to grep for "MGMT$DB"

The problem is, that "$" is used by the unix command shells to indicate the use+substitution of a unix shell/environment variable. SO, if you at the shell prompt, and said:

$ FFF="Myfile"
$ echo "Searching for MGMT$DB in $FFF..."

...you would see the output:

searching for MGMTFRED in Myfile...

The shell behavior depends upon what kind of QUOTES you use.

If you type:
$ echo "bla, bla $DB"
...the unix shell will see $DB in your string, and try to look
for an environment variable named DB, and modify the command
line's text, to substitute the text contents of that variable.
(so you get "bla, bla FRED" )

If you use SINGLE quoted strings, like:
$ echo 'bla, bla $DB'
$ grep 'MGMT$DB'
... then unix will leave your text string alone, and the echo or
grep command will see the exact/unchanged string, as you typed it.

For this reason, the double quotes (") are often referred to as "soft quotes" since the $VARIABLE string contents may be changed, before a command line is executed.
The single quotes (') are likeqise referred to as 'hard quotes' since the unix shell/interpreter will not change anything inside.

I hope that helps you understand the REASON, for castree's earlier response.

Side Note: If you WANT to use a unix/shell variable inside a "string", but have that variable name directly next to other text, then you need to use curly braces { } to surround the variable name. For instance, if you entered:
$ echo "status: $DB_OPEN"
...then you would NOT get "status: FRED_OPEN", because unix would not read that as $DB with "_OPEN" next to it... and would try to look for a variable named DB_OPEN (which does not exist).
Instead, you would delimit your variable name with { } and type:
$ echo "status: ${DB}_OPEN"
The braces will only be used to mark the variable name, and will not be printed, but unix will know exactly what part of your string is to be considered a variable name.

(to see all unix environment variables, try typing env or set )
Previous Topic: how to configure kernel parameter in HP unix
Next Topic: Trying to execute the shell script
Goto Forum:

Current Time: Sat Jul 26 10:49:07 CDT 2014

Total time taken to generate the page: 0.06951 seconds