Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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'`)
share|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
add comment

2 Answers 2

up vote 3 down vote accepted

Single quotes inhibit substitution. Use double quotes instead:

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

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`)
share|improve this answer
1  
You can't put spaces around the equal sign. –  Dennis Williamson Apr 19 '10 at 18:31
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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