bash: exec - Execute a command, replacing this shell with the specified program.



  • Take into account the main readme for this section. README first.

    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.
    

    Basic example of demonstrating the current shell is replaced with the command.

    🔴 IMPORTANT: If another shell is not started it will log you out your session, when running on the command line.

    One shell running.

    $ ps
      PID TTY          TIME CMD
     2299 pts/0    00:00:00 bash
     2928 pts/0    00:00:00 ps
    

    Start another shell.

    $ bash
    $ ps
      PID TTY          TIME CMD
     2299 pts/0    00:00:00 bash
     2929 pts/0    00:00:00 bash
     2951 pts/0    00:00:00 ps
    

    Use exec to replace current shell with command.

    $ exec uptime
     07:17:46 up  1:00,  1 user,  load average: 0.00, 0.00, 0.00
    

    The second shell that was started was replaced by the uptime command and exited.

    $ ps
      PID TTY          TIME CMD
     2299 pts/0    00:00:00 bash
     2952 pts/0    00:00:00 ps
    

Log in to reply
 

© Lightnetics 2024