The exec
command in Bash is used to execute a command in place of the current shell process. This means that when you run a command with exec
, the current shell is replaced by the command you specified, and it does not return to the original shell after the command completes.
The basic syntax of the exec
command is as follows:
exec [options] [command [arguments]]
-a
: Allows you to specify an alternative name for the command being executed.-l
: Makes the command a login shell.-p
: Preserves the environment of the current shell.exec bash
This command replaces the current shell with a new instance of Bash.
exec ./myscript.sh
This executes myscript.sh
and replaces the current shell with the script.
exec -a myalias /bin/ls -l
This runs the ls
command with an alias of myalias
, showing the long format of the directory listing.
exec -l bash
This command starts a new Bash shell as a login shell, replacing the current shell.
exec
when you want to run a command that should take over the current shell session, especially in scripts.exec
, as it will not return to the original shell after execution; any commands following exec
in the script will not be executed.exec
.