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
Replacing the Current Process: When
execis 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.No Subshell Creation: Unlike using just the command itself (e.g.,
command arguments), which creates a subshell to run the command,execruns the command within the current shell process. This can be useful for efficiency, especially when you want to avoid unnecessary subshell overhead.Use Cases:
Shell Scripting: In scripts,
execcan 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
execto replace built-in commands of the shell with external commands.
Examples
Running a Command
exec ls -lThis replaces the current shell process with
ls -l, listing files and directories in long format.
Executing a Shell Script
exec ./myscript.shThis replaces the current shell with the execution of
myscript.sh, assumingmyscript.shhas executable permissions (chmod +x myscript.sh).
Replacing Shell with Another Shell
exec bashThis replaces the current shell (e.g.,
sh,dash, etc.) withbash, effectively switching to a different shell environment.
Redirecting Standard Output
exec > output.txtThis 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 -DThis replaces the current shell with the
sshddaemon, running in the foreground (-Doption).
Setting Environment Variables
exec env VAR=value ./myprogramThis runs
myprogramwith an environment variableVARset tovalue, effectively setting up the environment before execution.
Considerations
No Return: Once
execis executed, it does not return unless there is an error in executing the command. This means any commands or scripts afterexecin a script will not be executed.Effect on Script Execution: Use
execcarefully 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