Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

Possible Duplicate:
external variable in awk

How do I pass this variable below?

This doesn't work:

fname=testfile.txt
lsof | awk '/deleted/&&/$fname/ {print $4}'  *----no output*

While this works:

lsof | awk '/deleted/&&/testfile.txt/ {print $4}'
3r
share|improve this question
    
use double quotes instead of single quotes –  Rahul Patil Dec 20 '12 at 10:50
add comment

marked as duplicate by Mat, manatwork, munish, Renan, glenn jackman Dec 20 '12 at 14:58

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

1 Answer

up vote 1 down vote accepted

This happens because shell variables are not expanded in single quotes. Use double quotes, like so:

fname=testfile.txt
lsof | awk "/deleted/&&/$fname/ {print \$4}"

Alternatively, you can pass it in as a variable to awk upon invocation using the -v switch.

share|improve this answer
    
double does not give the expected result –  munish Dec 20 '12 at 11:15
    
@munish You're probably missing the backslash escape. –  Chris Down Dec 20 '12 at 11:20
    
thanks, my mistake –  munish Dec 20 '12 at 11:23
add comment

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