How to pass arguments to /bin/bash? [closed]

孤者浪人 提交于 2019-12-11 02:59:00

问题


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.


回答1:


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.)




回答2:


-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'


来源:https://stackoverflow.com/questions/23025355/how-to-pass-arguments-to-bin-bash

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!