windowsperlcmdexecwindows-11

Perl on Windows not executing MD command in back-ticks


I found Perl installed on my PC:

C:\Users\matth>perl -v

This is perl 5, version 38, subversion 2 (v5.38.2) built for x86_64-msys-thread-multi
[...]

It came in with installing Git. I never did Perl on Windows by now, only on Linux, but now I could use it to simplify things a bit. I used backticks a lot on Linux to run commands.

On Windows, I try to create a directory using Windows’ MD ("make directory") command in back-ticks. I get an error. And I get a different error if there is a backslash in the path.

C:\Users\matth>perl -e 'use warnings; `md $ARGV[0]`;' test12
Can't exec "md": No such file or directory at -e line 1.

C:\Users\matth>perl -e 'use warnings; `md $ARGV[0]`;' test12\b4
sh: line 1: md: command not found

What is happening here? Can I call Windows commands in backticks, as if I was working on the CMD.exe command line manually? Or am I in a bash context? If so, always or just sometimes? When?


Solution

  • First of all, you talk of Windows, but you're using an MSYS build of Perl ("built for x86_64-msys-thread-multi"). MSYS provides a unix-ish environment.


    Why they fail


    Correct approaches

    From most complicated (what you were attempting) to least.

    Note that I'm using system instead of backticks because we're not trying to capture the output.