Home > Net >  Include the result of cat into a command in a Makefile (Windows)
Include the result of cat into a command in a Makefile (Windows)

Time:08-17

I have a Makefile with the following recipe:

test:
   echo "$$(cat ~/.ssh/id__something)"

If I run make test on MacOS (I assume any UNIX like system), this will echo the content on the file preserving line breaks, etc.

But If I run this on Windows I get the following output:

"$(cat ~/.ssh/id__something)"

Meaning, the cat command wasn't executed. Note that I'm using power shell, and cat is an alias for Get-Content.

Note that If I do cat ~/.ssh/id__something I get the expected result.

So, my question would be how to get the result of the command inside the makefile recipe.

Thanks in advance

CodePudding user response:

On Windows, make uses cmd.exe as the default shell for invoking shell commands, not PowerShell - this is independent of what shell you happen to be calling make from.

You can instruct make to use Windows PowerShell instead, by placing the following at the top of your Makefile:

SHELL := powershell.exe
.SHELLFLAGS := -NoProfile -Command 

While doing so enables you to use PowerShell-specific syntax in your make files, formulating cross-platform commands won't be possible, except in very simple cases: while PowerShell offers aliases for its cmdlets named for standard Unix utilities (e.g., cat for Get-Content, ls for Get-ChildItem), the fundamentally different syntax of these cmdlets compared to their Unix counterparts makes platform-agnostic calls all but impossible.

Using PowerShell syntax, you could then pass the content of a file as a multi-line string as follows:

test:
   echo (Get-Content -Raw ~/.ssh/id__something)
  • Related