I have a C program which uses argv inside the program. I understand that argv is the path of the program being executed. I want to pass a custom string as argv to the program instead of its program name. Is there a way to do this in shell?
I read about exec command. But I am unsure about the usage.
exec -a <string>
while true;do ./prog1 2; done & while true; do killall -14 prog1; done
system(echo something argv)
I need /bin/myprogram
exec -a is precisely the way to solve this problem.
There are no restrictions that I know of on the string passed as an argument to
exec. Normal shell quoting should be sufficient to pass anything you want (as long as it doesn't contain embedded NUL bytes, of course).
The problem with
exec is that it replaces the current shell with the named command. If you just want to run a command, you need to spawn a new shell to be replaced; that is as simple as surrounding the command with parentheses:
$ ( exec -a '; /bin/myprogram' bash -c 'echo "$0"'; ) ; /bin/myprogram