2

In my terminal, I do $ myprogram myfile.ext.

I want to be able to do the same via the command /bin/bash, but it is not working.

I tried

$ /bin/bash -c -l myprogram myfile.ext

but it is not working. It seems that my program is launched but without my file.ext as an option (as an input file)

Does anyone has a clue on how to do that?


Why I ask this question

I want to launch myprogram via NSTask in an a program I am writing for OSX, with the cocoa framework. If I simply ask NSTask to launch my program, it seems that some variable environment are missing. So, what I try is to launch a shell and inside this shell to launch myprogram.

Community
  • 1
  • 1
Colas
  • 3,473
  • 4
  • 29
  • 68
  • Please give the full context of what you're trying to do including any environment conditions that make this a special case. Based on your comments to answers this is not just a case of invoking bash with a filename to run. – kojiro Apr 13 '14 at 00:17

2 Answers2

3

Drop the -c altogether. From the manpage:

bash [options] [file]
…
Bash  is  an  sh-compatible command language interpreter that executes commands read from the standard input or from a file.

Thus, you can execute your program via

bash myprogram myfile.ext

and myfile.ext will be the first positional parameter.

bash -l myprogram myfile.ext

will work as well, (but whether or not bash is invoked as a login shell seems tangential to this question.)

kojiro
  • 74,557
  • 19
  • 143
  • 201
1
-c string 
    If  the  -c  option  is  present, then commands are read from
    string.  If there are arguments after the  string,  they  are
    assigned to the positional parameters, starting with $0.

You need to quote the command passed in as string so it's treated as a single argument for the -c option, which then gets executed normally with the argument following the command, i.e.

/bin/bash -c -l 'myprogram myfile.ext'
Reinstate Monica Please
  • 11,123
  • 3
  • 27
  • 48