Home how to pass command-line arguments to a program run with the open command?
Reply: 3

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

Francisco Aguilera
Francisco Aguilera Published in 2015-04-08 09:27:08Z

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.


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

open -a /Applications/Utilities/Terminal.app [~/my_executable arg1 arg2]
Paul R
Paul R Reply to 2015-04-08 09:39:19Z

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
Enrico Reply to 2016-03-08 15:21:16Z

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.

Jean-François Corbett
Jean-François Corbett Reply to 2018-01-17 21:38:22Z

Yes, I know. need to manage another script. but think differently. you work not on Terminal, but on Script Editor. (not bash scripting, but AppleScript'ing)

property testScript : "/tmp/sh.sh"

set input to display dialog "args?" default answer ""
log input
tell application "Terminal"
    do script testScript & " " & text returned of input
end tell
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.387291 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO