Ubuntu – Run program from a shell script but behave as one process only

bashprocessprogramming

Is there a way I can execute an application from a shell script but not create another process. I want it to look like one process only. It doesn't matter whether my shell script is replaced by a new process or whether it will continue after a called application ends.

This should also solve my previous question: https://askubuntu.com/questions/247632/is-there-a-way-to-associate-additional-application-launcher-with-an-app

Thank you very much for your help.

Best Answer

You can use the exec command:

$ help exec
exec: exec [-cl] [-a name] [command [arguments ...]] [redirection ...]
    Replace the shell with the given command.

    Execute COMMAND, replacing this shell with the specified program.
    ARGUMENTS become the arguments to COMMAND.  If COMMAND is not specified,
    any redirections take effect in the current shell.

    Options:
      -a name   pass NAME as the zeroth argument to COMMAND
      -c        execute COMMAND with an empty environment
      -l        place a dash in the zeroth argument to COMMAND

    If the command cannot be executed, a non-interactive shell exits, unless
    the shell option `execfail' is set.

    Exit Status:
    Returns success unless COMMAND is not found or a redirection error occurs.

Example:

user@host:~$ PS1="supershell$ "
supershell$ bash
user@host:~$ PS1="subshell$ "
subshell$ exec echo hello
hello
supershell$ 

As you can see, the subshell is replaced by echo.

Related Question