Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to pass command-line arguments to a program run with the open command?

Tags:

bash

macos

Is there a way to pass arguments to a program being run via:

open -a /Applications/Utilities/Terminal.app ~/my_executable

I have tried:

open -a /Applications/Utilities/Terminal.app ~/my_executable arg1 arg2

But this is interpreted as telling the terminal to open ~/my_executable ~/arg1 ~/arg2.

I have tried:

open -a /Applications/Utilities/Terminal.app '~/my_executable arg1 arg2'

But it picks up arg1 and arg2 as if they were part of the path rather than arguments.

I have tried:

open -a /Applications/Utilities/Terminal.app ~/my_executable | xargs arg1 arg2

I have also tried:

open -a /Applications/Utilities/Terminal.app ~/my_executable --args arg1 arg2

But with that flag, args are passed to the terminal.

NOTE

I am only allowed to change the arguments to Terminal.app (the part within [ ]):

open -a /Applications/Utilities/Terminal.app [~/my_executable arg1 arg2]
like image 428
Francisco Aguilera Avatar asked Apr 08 '15 09:04

Francisco Aguilera


People also ask

How do I pass a command line argument to a program?

If you want to pass command line arguments then you will have to define the main() function with two arguments. The first argument defines the number of command line arguments and the second argument is the list of command line arguments.

How do I run a program from the command line in Windows?

Type "start [filename.exe]" into Command Prompt, replacing "filename" with the name of your selected file. Replace "[filename.exe]" with your program's name. This allows you to run your program from the file path.

How do you run with an argument?

If you open any folder, then the shortest way to run any application with arguments is just typing program name and arguments in the upper textbox. Otherwise, if your program is on Desktop, you can use various ways such as: Click on any folder on your desktop, up one level, and do the same as is in picture.


2 Answers

Edit: Leaving the original answer below as some people seem to find it useful, but keep in mind that this doesn't really answers OP's question, this is to pass arguments to an app opened with "open" not to and app opened with Terminal.app which was opened with "open".

You can find your answer by running open without arguments:

% open Usage: open [-e] [-t] [-f] [-W] [-R] [-n] [-g] [-h] [-b <bundle identifier>] [-a <application>] [filenames] [--args arguments]

[...]

--args All remaining arguments are passed in argv to the application's main() function instead of opened.

[...]

You can see there is an option --args you can use it like this:

open ./Untitled.app --args arg1 arg2 arg3

I tested it on el Capitan (10.11.3) so I don't know if the option is present in earlier versions.

like image 187
Enrico Avatar answered Nov 01 '22 12:11

Enrico


Probably the easiest way is to create a temporary shell script, e.g.

$ echo "~/my_executable arg1 arg2" > /tmp/tmp.sh ; chmod +x /tmp/tmp.sh ; open -a Terminal /tmp/tmp.sh ; rm /tmp/tmp.sh
like image 43
Paul R Avatar answered Nov 01 '22 11:11

Paul R