How to make a variable in a shell script program equal to the output from a command.

Hi,

I have a command like

grep "good work" myfile.results | wc -l

in my bash shell script.

I normally output this to a file, but I would prefer to be able to put this into a variable I can keep (and then modify).

Any idea how to do this?

i tried
hello=grep "good work" myfile.results | wc -l

but that didn't work :(

Thanks.

-Edward
edwardtAsked:
Who is Participating?
 
ren_bConnect With a Mentor Commented:
hello=`grep "good work" myfile.results | wc -l`
0
 
edwardtAuthor Commented:
Hi,
On my file, I need to reference some vairalbles too, but it seems

hello='grep "good work" "$1${i}.results" | wc -l'

doesn't work.

Thanks.

-Edward
0
 
ren_bCommented:
don't use the ' (single quote), you have to use the ` (backtick).
0
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

 
edwardtAuthor Commented:
also, when I try to do
hello=`grep "good work" myfile.results | wc -l`
and i do echo "hello is ${hello}" i get the string.

Thanks.

Edward
0
 
edwardtAuthor Commented:
Ah, thanks! :)

-Edward
0
 
ren_bCommented:
> also, when I try to do
> hello=`grep "good work" myfile.results | wc -l`
> and i do echo "hello is ${hello}" i get the string.

that's what you want isn't it?
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.