exec

exec

In Unix-like operating systems, the exec command is a built-in shell command that replaces the current process with a new program. It is used primarily for executing commands in the current shell session, rather than spawning a new process.

Basic Syntax

exec command [arguments]
  • command: The command to execute.

  • arguments: Optional arguments passed to the command.

Key Points

  1. Replacing the Current Process: When exec is used, the current shell process is replaced by the specified command. This means that the new command effectively takes over the current shell environment, including its PID (Process ID), standard input, output, and error streams.

  2. No Subshell Creation: Unlike using just the command itself (e.g., command arguments), which creates a subshell to run the command, exec runs the command within the current shell process. This can be useful for efficiency, especially when you want to avoid unnecessary subshell overhead.

  3. Use Cases:

    • Shell Scripting: In scripts, exec can be used to replace the current shell with a different command or script, potentially with different privileges or environment settings.

    • Shell Built-in Commands: You can use exec to replace built-in commands of the shell with external commands.

Examples

Running a Command

exec ls -l
  • This replaces the current shell process with ls -l, listing files and directories in long format.

Executing a Shell Script

exec ./myscript.sh
  • This replaces the current shell with the execution of myscript.sh, assuming myscript.sh has executable permissions (chmod +x myscript.sh).

Replacing Shell with Another Shell

exec bash
  • This replaces the current shell (e.g., sh, dash, etc.) with bash, effectively switching to a different shell environment.

Redirecting Standard Output

exec > output.txt
  • This redirects all subsequent standard output in the current shell session to output.txt.

Practical Use Cases

Replacing a Process with a Daemon

exec /usr/sbin/sshd -D
  • This replaces the current shell with the sshd daemon, running in the foreground (-D option).

Setting Environment Variables

exec env VAR=value ./myprogram
  • This runs myprogram with an environment variable VAR set to value, effectively setting up the environment before execution.

Considerations

  • No Return: Once exec is executed, it does not return unless there is an error in executing the command. This means any commands or scripts after exec in a script will not be executed.

  • Effect on Script Execution: Use exec carefully in scripts, as it directly affects the current shell environment and process. Ensure that the intended command and its options are correct to avoid unintended consequences.

Conclusion

The exec command in Unix-like systems is a powerful tool for shell scripting and managing processes. It allows you to efficiently replace the current shell process with a new command or script, making it useful for tasks ranging from running daemons to managing environment variables in scripts.

help

exec: exec [-cl] [-a name] [command [argument ...]] [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
    

breakdown

-l, --login: This option causes the exec command to start a new login shell. A login shell is a shell that is started when a user logs in to a system.
-c, --command: This option causes the exec command to execute the specified command and then exit.
-a, --append: This option causes the exec command to append the specified command to the current shell's command line.
-h, --help: This option shows this help message.

Last updated