Reputation: 19284
This correctly prints test
$ echo 'this is a test' | awk '{print $4}'
test
While using this command inside /bin/bash -c
does not work
/bin/bash -c "echo 'this is a test' | awk '{print $4}'"
this is a test
How can I get awk to work correctly when using with /bin/bash -c
?
Upvotes: 2
Views: 625
Reputation: 784948
$4
is expended by shell since you have double quoted command string.
You can check trace output by adding -x
in bash
command line:
bash -xc "echo 'this is a test' | awk '{print $4}'"
+ echo 'this is a test'
+ awk '{print }'
this is a test
Since $4
expands to an empty string it effectively runs awk '{print }'
and thus complete line is printed in output.
To fix this, you should be using an escaped $
to avoid this expansion:
bash -c "echo 'this is a test' | awk '{print \$4}'"
test
Upvotes: 5