0

In Windows 10 with PowerShell 7.3.3. I have a program foo.exe on my path. I can invoke it like this:

foo test

I want to insert the parameter --bar before all arguments. My first thought is to create a function in my $PROFILE like this:

function foo {
  foo --bar @args
}

Obviously that doesn't work, because it results in an endless loop (the function calls itself).

How can I invoke the command/program foo from within the function foo? I'm guessing I can research and do something like the Bash $(pwd) and then create a path to the original executable, and hopefully that would bypass the function. But is there an easier way?

Garret Wilson
  • 18,219
  • 30
  • 144
  • 272
  • You could call the file with extension bat calling an exe. Also see following : https://stackoverflow.com/questions/14311606/how-to-execute-a-bat-file-first-instead-of-an-file-exe-when-both-have-the-same?force_isolation=true – jdweng Mar 16 '23 at 17:21
  • Just call the program by name including extension and use `$` or `@` in place of `%`. Try this: `function foo { foo.exe --bar @args }`. – zett42 Mar 16 '23 at 17:53
  • Thanks. I had thought of the `foo.exe` trick when I stepped away. If one of you wants to write that up in an official answer, I'll accept it. Otherwise I'll write up an answer myself. – Garret Wilson Mar 16 '23 at 19:54
  • This is a pretty generic question, so there is propably already an existing Q&A. – zett42 Mar 16 '23 at 22:05
  • Unfortunately my question was a little too generic, because what I was really wanting to do was to pass `--%` instead of `--bar`, which doesn't work. ☹ See comments to https://stackoverflow.com/a/73650127 . – Garret Wilson Mar 16 '23 at 22:10
  • doing something like `& { $args = @('--%'; $args ); .\print_argv.exe @args } foo bar baz` resolves correctly for me: https://i.imgur.com/wsOt00O.png might work for you too – Santiago Squarzon Mar 17 '23 at 00:03
  • Probably a better example, this is what you're looking to pass to java according to the comments in that answer: https://i.imgur.com/gZW7drG.png – Santiago Squarzon Mar 17 '23 at 00:06
  • Santiago, I didn't understand your example, which showed two arguments of `-Dfoo` and `.bar=test`, because the whole point of that exercise was to pass `-Dfoo.bar=test` as a single argument, working around PowerShell's bug that erroneously splits an argument on the dot. – Garret Wilson Mar 17 '23 at 01:34

0 Answers0