3

In Linux / Unix shells there is this syntax where you can execute a command and in this command are other commands which are executed first and then substituted so something like this:

gcc main.c `pkg-config --cflags --libs gtk+-3.0`

Here the pkg-..... gets executed first and then substituted with its output and then the overall command is executed

Is there some similar functionality in Windows PowerShell (and if possible cmd too as I work with both sometimes)

All that I know is that in PowerShell you can write something like this:

gcc main.c (pkg-config --cflags --libs gtk+-3.0)

But the problem with this is that the output of the secondary command is passed like a single continuous string so something like this "-mms-bitfields ....." and gcc doesn't recognize it as separate commands.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
Lockon2000
  • 343
  • 1
  • 3
  • 14
  • Why is this question tagged c? – Iharob Al Asimi Apr 09 '15 at 13:23
  • Well, because this is a question any c programmer on windows would like to know at least from my point of view. I could remove it if you want. – Lockon2000 Apr 09 '15 at 13:28
  • From what I know it's really hard to use the PowerShell or any windows shell with the same features that *nix shells have, mainly because there are a lot of programs in the *nix world that do very nice things to make life easier, when I thy to use windows shell I feel like my fingers are heavy, It's my opinion though and might very will be wrong. – Iharob Al Asimi Apr 09 '15 at 13:31

1 Answers1

3

You can use the -split operator:

gcc main.c ((pkg-config --cflags --libs gtk+-3.0) -split " ")

Or possibly

gcc main.c ((pkg-config --cflags --libs gtk+-3.0) -split " +")

to avoid empty arguments in case there are multiple spaces between arguments in the output (although I don't think pkg-config does that).

The -split operator does precisely what it says on the tin: It splits what it is given at occurrences of the given pattern. That is to say,

PS C:\> "foo bar baz" -split " "
foo
bar
baz
Wintermute
  • 42,983
  • 5
  • 77
  • 80
  • Thanks, this was exactly what I was looking for. is there anything similar for CMD. – Lockon2000 Apr 09 '15 at 13:39
  • 2
    Not off the top of my head, I'm afraid. I try not to do that sort of thing in cmd. Happily, with the death of Windows XP, you can rely on the presence of Powershell. – Wintermute Apr 09 '15 at 13:40
  • ok, I think I know what you mean. anyway thanks again for your help. – Lockon2000 Apr 09 '15 at 13:42
  • 1
    There's a workaround for cmd in [this question](http://stackoverflow.com/questions/2768608/batch-equivalent-of-bash-backticks); it does give me an extra empty argument at the end if the output ends with a newline, though. – Wintermute Apr 09 '15 at 13:46
  • I appreciate your help, but maybe I should really concern moving completely to PowerShell as you suggested. – Lockon2000 Apr 09 '15 at 13:57