Operating System - HP-UX
1833648 Members
4722 Online
110062 Solutions
New Discussion

Re: Shell Variable Problem

 
SOLVED
Go to solution
Greg Stark_1
Frequent Advisor

Shell Variable Problem

I want to search all the files in directory for the word N O R T H. If I run

grep -i "N O R T H" *

from the command line, it finds the files with the word in it.

If I do this

export VAR="N O R T H"
grep -i $VAR *

I get content from every file in the directory.

I've also tried exporting the VAR like this

export VAR='"N O R T H"'

But then it searches for each letter, not the entire word.

Any ideas how to set this variable so the shell interprets it properly?

Thanks again,
Greg

5 REPLIES 5
Steven E. Protter
Exalted Contributor

Re: Shell Variable Problem

try the -e (expression option)

grep -i -e $VAR

SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
RAC_1
Honored Contributor

Re: Shell Variable Problem

Check for yourself. It differs.

echo `grep -i "N O R T H" *`
export VAR="N O R T H"
echo `grep -i $VAR *`

Anil
There is no substitute to HARDWORK
Francisco J. Soler
Honored Contributor
Solution

Re: Shell Variable Problem

Hi Greg,
try:
export VAR="N O R T H"
grep -i "$VAR" *
or even
grep -i "${VAR}" *

Frank.
Linux?. Yes, of course.
Bill Hassell
Honored Contributor

Re: Shell Variable Problem

The problem is that you tell the shell to treat the contents of a variable as a single string too. When you just plunk $VAR into a command line, the shell expands it to it's value but doesn't know that you wnat the entire contents treated as a single string. Always enclose the use of a variable containing special characters with "" as in grep -i "$VAR"


Bill Hassell, sysadmin
Greg Stark_1
Frequent Advisor

Re: Shell Variable Problem

Thanks all.