How can I use bash
syntax in Perl's system()
command?
I have a command that is bash-specific, e.g. the following, which uses bash's process substitution:
diff <(ls -l) <(ls -al)
I would like to call it from Perl, using
system("diff <(ls -l) <(ls -al)")
but it gives me an error because it's using sh
instead of bash
to execute the command:
sh: -c: line 0: syntax error near unexpected token `('
sh: -c: line 0: `sort <(ls)'
should do it, in theory. Bash's
-c
option allows you to pass a shell command to execute, according to the man page.The problem with vladr's answers is that system won't capture the output to STDOUT from the command (which you would usually want), and it also doesn't allow executing more than one command (given the use of shift rather than accessing the full contents of @_).
Something like the following might be more suited to the problem:
Unfortunately this won't prevent you from invoking /bin/sh just to run bash, however I don't see a workaround for this issue.
Tell Perl to invoke bash directly. Use the
list
variant ofsystem()
to reduce the complexity of your quoting:You may even define a subroutine if you plan on doing this often enough:
I prefer to execute bash commands in perl with backticks "`". This way I get a return value, e.g.:
Also, I don't have to use "bash -c" just to run a commmand. Works for me.
Inspired by the answer from @errant.info, I created something simpler and worked for me:
The introduced $line variable allows modifying inputs ($seq1 and $seq2) each time. Hope it helps!