in reply to Re: Effect of redirecting output to /dev/null on $? value
in thread Effect of redirecting output to /dev/null on $? value

How is the command executed if the shell is not invoked?
Why does adding a redirect to /dev/null increase the complexity enough to invoke the shell?
Finally, what method is used to execute the command if I had instead used perl's system() function instead of the backticks?
  • Comment on Re^2: Effect of redirecting output to /dev/null on $? value

Replies are listed 'Best First'.
Re^3: Effect of redirecting output to /dev/null on $? value
by hippo (Archbishop) on Aug 03, 2020 at 15:44 UTC

    This is pretty much all covered in the docs for system().

    Why does adding a redirect to /dev/null increase the complexity enough to invoke the shell?

    Because it's the shell that does the redirection.


    🦛

Re^3: Effect of redirecting output to /dev/null on $? value
by ikegami (Patriarch) on Aug 03, 2020 at 17:01 UTC

    How is the command executed if the shell is not invoked?

    By asking to system execute the specified program instead of asking the system to execute /bin/sh.

    Why does adding a redirect to /dev/null increase the complexity enough to invoke the shell?

    There's no point in including an entire shell in Perl. I don't know exactly what part of shell command parsing is implemented in Perl.

    Finally, what method is used to execute the command if I had instead used perl's system() function instead of the backticks?

    system($cmd), exec($cmd), open(my $pipe, '-|', $cmd) (and its 2-arg form) and open(my $pipe, '|-', $cmd) (and its 2-arg form) work the same way as `$cmd` (aka qx`$cmd` aka readpipe($cmd)).

    system($prog, LIST) where LIST returns at least one scalar won't invoke the shell.[1]

    system({ $prog } $prog, LIST) won't invoke the shell.


    1. It can in Windows.