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.

Is there a way to make awk use bash instead of sh when running system commands using the system() call?

I want to use some bash-specific features such as [[ ]] and < > string comparison operators:

gawk 'BEGIN {system("if [[ \"porc\" == \"porc\" ]]; then echo yes; else echo no; fi")}'
sh: [[: not found
share|improve this question
1  
Awk is quite a powerful utility. There is nothing so special about the features you mention that awk cannot do... eg. here is a simpler way to compare strings without the need for an unnecessary system call and the nuisance quoting. awk 'BEGIN{ if("abc" > "abcd") {print "yes"} else {print "no"} } ..(+1 for the question, because is it good to know how to do it, and maybe why not to do it :) –  Peter.O Jun 16 '12 at 21:19

1 Answer 1

up vote 4 down vote accepted

If you want bash, you'll either have to patch gawk or call bash directly (the quoting gets a bit hairy):

gawk 'BEGIN {
    script="if [[ \"porc\" == \"porc\" ]]; then echo yes; else echo no; fi"
    system("bash -c '\''" script "'\''")
}'
share|improve this answer
    
if those are the only options... –  haelix Jun 16 '12 at 19:31

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.