Perl backticks: use bash instead of sh

I noticed that when I use backticks in perl, commands are executed using sh rather than bash, which gives me some problems.

How can I change this behavior, so perl will use bash?

PS. The command I'm trying to run is:

paste filename <(cut -d \" \" -f 2 filename2 | grep -v mean) >> filename3
+5
source share
6 answers

Try

`bash -c \"your command with args\"`

I'm sure the -c argument is interpreted the way bash interprets its command line. The trick is to protect it from sh- for what quotes.

+7
source

The "system shell" is usually not changed. See perldoc -f exec :

LIST LIST , execvp (3) LIST. , , , ( "/bin/sh -c" Unix, ).

bash , , :

my $result = `/usr/bin/bash command arguments`;

:

open my $bash_handle, '| /usr/bin/bash' or die "Cannot open bash: $!";
print $bash_handle 'command arguments';

bash .sh :

my $result = `/usr/bin/bash script.pl`;
+7

:

$ perl -e 'print `/bin/bash -c "echo <(pwd)"`'
/dev/fd/63
+4

bash , : bash Perl()?

my @args = ( "bash", "-c", "diff <(ls -l) <(ls -al)" );
system(@args);
+1

I thought I perlwould evaluate a variable $SHELL, but then it occurred to me that its behavior might depend on your system exec. In mine, it seems thatexec

will execute a shell (/ bin / sh) with the path as the first argument.

You can always do qw/bash your-command/, no?

0
source

Create a perl routine:

sub bash { return `cat << 'EOF' | /bin/bash\n$_[0]\nEOF\n`; }

And use it as below:

my $bash_cmd = 'paste filename <(cut -d " " -f 2 filename2 | grep -v mean) >> filename3';
print &bash($bash_cmd);

Or use perl here-doc for multi-line commands:

$bash_cmd = <<'EOF';
    for (( i = 0; i < 10; i++ )); do
       echo "${i}"
    done
EOF
print &bash($bash_cmd);
0
source

All Articles