In my bash script i want to execute command that will find lines in a file that starts with a keyword which is stored in a variable like this:

keyword="key" 
result=(`cat path/to/my/file | grep '^$keyword'`)
link|improve this question
Useless use of 'cat' :) grep $keyword < path/to/my/file – jscott Apr 19 '10 at 18:23
2  
@jscott: useless use of redirection – Dennis Williamson Apr 19 '10 at 18:29
@Dennis: Indeed. I'm not sure the comments parse the ':)' humor markdown correctly. – jscott Apr 19 '10 at 19:45
feedback

2 Answers

up vote 3 down vote accepted

Single quotes inhibit substitution. Use double quotes instead:

grep "^$keyword"
link|improve this answer
oh, thanks it is even better solution – Skuja Apr 19 '10 at 18:18
feedback

Forgive me please, i figured it all out as soon as i posted my question: If has simular issue:

keyword="key" 
find_cmd = "cat path/to/my/file | grep '^$keyword'"
result=(`$find_cmd`)
link|improve this answer
1  
You can't put spaces around the equal sign. – Dennis Williamson Apr 19 '10 at 18:31
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.